在 R 中声明 Const 变量
我在 R 中工作,我想定义一些我(或我的合作者之一)无法更改的变量。 在 C++ 中,我会这样做:
const std::string path( "/projects/current" );
How do I do this in the 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 编程语言中也是不可能的。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
请参阅lockBinding:
See
lockBinding
:由于您计划将代码分发给其他人,因此您可以(应该?)考虑创建一个包。 在该包中创建一个命名空间。 您可以在那里定义具有常量值的变量。 至少到你的包使用的功能。 查看 Tierney (2003) R 的命名空间管理
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
我很确定这在 R 中是不可能的。如果您担心不小心重写该值,那么最简单的方法是将所有常量放入列表结构中,然后您就知道什么时候“正在使用这些值。 就像这样:
然后,当您需要访问它们时,您有一个辅助回忆录来知道不该做什么,并且它将它们推出您的正常名称空间。
另一个可以询问的地方是 R 开发邮件列表。 希望这可以帮助。
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:
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.
(为新想法进行编辑:)
bindenv
函数提供了这似乎是一种可能给人一种错误的安全感的东西(就像指向非 const 变量的 const 指针),但它可能会有所帮助。
(针对焦点进行编辑:)
const
是一个编译时保证 ,而不是锁定内存中的位。 由于 R 没有同时查看所有代码的编译阶段(它是为交互式使用而构建的),因此无法检查未来的指令是否不会违反任何保证。 如果有正确的方法可以做到这一点,R-help 的人们名单就知道了。 我建议的解决方法:伪造你自己的编译。 编写一个脚本来预处理您的 R 代码,该代码将手动替换每次出现的“常量”变量的相应文字。(原文:)您希望从具有类似于 C“const”的变量中获得什么好处?
由于 R 独有按值调用语义 (除非你对环境进行一些修改),没有任何理由担心通过调用函数来破坏你的变量。 如果您担心您和您的协作者不小心使用同名的变量,那么采用某种命名约定或使用某种 OOP 结构可能是正确的解决方案。
您正在寻找的功能可能存在,但我怀疑它的起源,因为 R 最初是一个交互式环境,您希望能够在其中撤消您的操作。
(Edited for new idea:) The
bindenv
functions provide anThis 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.
R 没有语言常量功能。 上面的列表想法很好; 我个人使用像 ALL_CAPS 这样的命名约定。
R doesn't have a language constant feature. The list idea above is good; I personally use a naming convention like ALL_CAPS.
得到了下面的答案
我从这个网站 最简单的 R 表达式只是一个常量值,通常是数值(数字)或字符值(一段文本)。 例如,如果我们需要指定对应于10分钟的秒数,我们指定一个数字。
如果我们需要指定要从中读取数据的文件的名称,我们可以将该名称指定为字符值。 字符值必须用双引号或单引号引起来。
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.
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.