42

我在 R 中工作,我想定义一些我(或我的一位合作者)无法更改的变量。在 C++ 中,我会这样做:

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

我如何在 R 编程语言中做到这一点?

为清楚起见进行编辑:我知道我可以在 R 中定义这样的字符串:

path = "/projects/current"

我真正想要的是一种语言构造,它保证没有人可以更改与名为“path”的变量关联的值。

编辑以回应评论:

const 是编译时保证在技术上是正确的,但在我看来,R 解释器会抛出停止执行并显示错误消息是有效的。例如,看看当您尝试将值分配给数字常量时会发生什么:

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

所以我真正想要的是一种语言特性,它允许你只赋值一次,当你尝试为声明为 const 的变量赋值时应该会出现某种错误。我不在乎错误是否发生在运行时,尤其是在没有编译阶段的情况下。根据维基百科的定义,这在技术上可能不是 const ,但它非常接近。看起来这在 R 编程语言中也是不可能的。

4

6 回答 6

52

See lockBinding:

a <- 1
lockBinding("a", globalenv())
a <- 2
Error: cannot change value of locked binding for 'a'
于 2009-09-16T15:47:00.240 回答
10

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

于 2009-07-22T14:36:30.120 回答
4

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.

于 2009-06-02T09:45:33.960 回答
3

(为新想法编辑:)这些bindenv功能提供了一个

用于调整环境和环境内绑定的实验界面。它们允许锁定环境以及单独的绑定,以及将变量链接到函数。

这似乎是一种可能给人一种错误的安全感的东西(比如const指向非const变量的指针),但它可能会有所帮助。

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

于 2009-06-02T01:37:14.160 回答
1

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

于 2009-07-22T06:39:40.903 回答
-4

我从这个网站上得到了下面的答案

最简单的 R 表达式只是一个常量值,通常是数值(数字)或字符值(一段文本)。例如,如果我们需要指定对应于 10 分钟的秒数,我们指定一个数字。

> 600
[1] 600

如果我们需要指定要从中读取数据的文件的名称,我们将名称指定为字符值。字符值必须用双引号或单引号括起来。

> "http://www.census.gov/ipc/www/popclockworld.html"
[1] "http://www.census.gov/ipc/www/popclockworld.html"
于 2009-06-01T21:04:47.207 回答