确保函数不使用全局变量 [英] making sure a function does not use a global variable

查看:33
本文介绍了确保函数不使用全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

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

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"

这篇关于确保函数不使用全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆