Declaring a Const Variable in R

前端 未结 6 970
耶瑟儿~
耶瑟儿~ 2020-12-05 00:07

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( \"/         


        
相关标签:
6条回答
  • 2020-12-05 00:23

    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 a look at Tierney (2003) Name Space Management for R

    0 讨论(0)
  • 2020-12-05 00:25

    (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.

    0 讨论(0)
  • 2020-12-05 00:30

    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.

    0 讨论(0)
  • 2020-12-05 00:31

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

    0 讨论(0)
  • 2020-12-05 00:39

    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"
    
    0 讨论(0)
  • 2020-12-05 00:42

    See lockBinding:

    a <- 1
    lockBinding("a", globalenv())
    a <- 2
    Error: cannot change value of locked binding for 'a'
    
    0 讨论(0)
提交回复
热议问题