【问题标题】:making sure a function does not use a global variable [duplicate]确保函数不使用全局变量[重复]
【发布时间】:2014-01-20 22:56:37
【问题描述】:

这可能与类似的问题有点相反。如果代码中的任何地方,函数在父环境中使用变量,我希望 R 中止\警告。是否有一些基本选项可以实现这一目标?我想要一个对会话通用的解决方案,而不是特定的检查。 谢谢。

【问题讨论】:

  • 我想你会找到感兴趣的 codetools 包。
  • @Dason findGlobals() 似乎是我所寻找的。谢谢。

标签: r scope


【解决方案1】:

codetools 包中有一个函数findGlobals。也许这会有所帮助:

library(codetools)
x <- "global"
foo <- function() x

foo()
[1] "global"

findGlobals(foo)
[1] "x"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-12
    • 1970-01-01
    • 2015-07-10
    相关资源
    最近更新 更多