views:

636

answers:

6

I'm working in R, and I'd like to define some variables that I (or one of my collaborators) cannot change. In C++ I'd do this:

const std::string path( "/projects/current" );

How do I do this in the R programming language?

Edit for clarity: I know that I can define strings like this in R:

path = "/projects/current"

What I really want is a language construct that guarantees that nobody can ever change the value associated with the variable named "path."

Edit to respond to comments:

It's technically true that const is a compile-time guarantee, but it would be valid in my mind that the R interpreter would throw stop execution with an error message. For example, look what happens when you try to assign values to a numeric constant:

> 7 = 3
Error in 7 = 3 : invalid (do_set) left-hand side to assignment

So what I really want is a language feature that allows you to assign values once and only once, and there should be some kind of error when you try to assign a new value to a variabled declared as const. I don't care if the error occurs at run-time, especially if there's no compilation phase. This might not technically be const by the Wikipedia definition, but it's very close. It also looks like this is not possible in the R programming language.

A: 

I took the answer below from this website

The simplest sort of R expression is just a constant value, typically a numeric value (a number) or a character value (a piece of text). For example, if we need to specify a number of seconds corresponding to 10 minutes, we specify a number.

> 600
[1] 600

If we need to specify the name of a file that we want to read data from, we specify the name as a character value. Character values must be surrounded by either double-quotes or single-quotes.

> "http://www.census.gov/ipc/www/popclockworld.html"
[1] "http://www.census.gov/ipc/www/popclockworld.html"
Billy
You're confusing constants with variables that can't be changed once assigned.
David Locke
@David Locke - Good point. I tried to clarify this in the question.
James Thompson
+3  A: 

(Edited for new idea:) The bindenv functions provide an

experimental interface for adjustments to environments and bindings within environments. They allow for locking environments as well as individual bindings, and for linking a variable to a function.

This seems like the sort of thing that could give a false sense of security (like a const pointer to a non-const variable) but it might help.

(Edited for focus:) const is a compile-time guarantee, not a lock-down on bits in memory. Since R doesn't have a compile phase where it looks at all the code at once (it is built for interactive use), there's no way to check that future instructions won't violate any guarantee. If there's a right way to do this, the folks at the R-help list will know. My suggested workaround: fake your own compilation. Write a script to preprocess your R code that will manually substitute the corresponding literal for each appearance of your "constant" variables.

(Original:) What benefit are you hoping to get from having a variable that acts like a C "const"?

Since R has exclusively call-by-value semantics (unless you do some munging with environments), there isn't any reason to worry about clobbering your variables by calling functions on them. Adopting some sort of naming conventions or using some OOP structure is probably the right solution if you're worried about you and your collaborators accidentally using variables with the same names.

The feature you're looking for may exist, but I doubt it given the origin of R as a interactive environment where you'd want to be able to undo your actions.

othercriteria
I'm not worried about clobbering variables by calling functions, I'd just like to guarantee that certain variables provided in the environment are never changed. I'm basically defining a set of analysis tools, and I'm defining several constants for processing that shouldn't change throughout the program's execution. It looks like this might be tricky/impossible in R.
James Thompson
+2  A: 

I'm pretty sure that this isn't possible in R. If you're worried about accidentally re-writing the value then the easiest thing to do would be to put all of your constants into a list structure then you know when you're using those values. Something like:

my.consts<-list(pi=3.14159,e=2.718,c=3e8)

Then when you need to access them you have an aide memoir to know what not to do and also it pushes them out of your normal namespace.

Another place to ask would be R development mailing list. Hope this helps.

David Lawrence Miller
+1  A: 

R doesn't have a language constant feature. The list idea above is good; I personally use a naming convention like ALL_CAPS.

Brendan OConnor
+1  A: 

Since you are planning to distribute your code to others, you could (should?) consider to create a package. Create within that package a NAMESPACE. There you can define variables that will have a constant value. At least to the functions that your package uses. Have al look at Tierney (2003) Name Space Management for R

Thierry
+4  A: 

See lockBinding:

> a <- 1
> lockBinding("a", globalenv())
> a <- 2
Error: cannot change value of locked binding for 'a'
hadley