R 中的全局变量
我正在查阅手册,我想问社区: 我们如何在函数内设置全局变量?
I am poking into the manuals, I wanted to ask the community:
How can we set global variables inside a function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如 Christian 对
assign()
的回答所示,有一种在全局环境中进行分配的方法。 一种更简单、更短(但不是更好......坚持分配)的方法是使用<<-
运算符,即在函数内部。
As Christian's answer with
assign()
shows, there is a way to assign in the global environment. A simpler, shorter (but not better ... stick with assign) way is to use the<<-
operator, ieinside the function.
我找到了如何在 邮件列表发帖中设置全局变量的解决方案通过分配:
I found a solution for how to set a global variable in a mailinglist posting via assign:
.GlobalEnv$a <- "new"
怎么样? 我在这里看到了在特定环境中创建变量的显式方法: http://adv- r.had.co.nz/Environments.html。 它似乎比使用assign()
函数更短。What about
.GlobalEnv$a <- "new"
? I saw this explicit way of creating a variable in a certain environment here: http://adv-r.had.co.nz/Environments.html. It seems shorter than using theassign()
function.