making sure a function does not use a global varia

2019-01-28 05:37发布

问题:

This question already has an answer here:

  • R force local scope 11 answers

This maybe a bit converse to similar questions. I would like R to abort\warn if anywhere in the code, a function uses a variable in a parent environment. Is there some base option to achieve that? I would like a solution that is general for a session, not a particular check. Thank you.

回答1:

There is a function findGlobals in the codetools package. Maybe this is helpful:

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

foo()
[1] "global"

findGlobals(foo)
[1] "x"


标签: r scope