在采购时,有没有办法在R中"检查"或"验证"源代码文件?例如,我在文件"source.R"中有此功能
MyFunction <- function(x)
{
print(x+y)
}
Run Code Online (Sandbox Code Playgroud)
在采购"source.R"时,我希望看到某种警告: MyFunctions refers to an undefined object Y.
有关如何检查/验证R代码的任何提示?
干杯!
flo*_*del 10
我使用像这样的函数来扫描文件中的所有函数:
critic <- function(file) {
require(codetools)
tmp.env <- new.env()
sys.source(file, envir = tmp.env)
checkUsageEnv(tmp.env, all = TRUE)
}
Run Code Online (Sandbox Code Playgroud)
假设source.R包含两个写得很差的函数的定义:
MyFunction <- function(x) {
print(x+y)
}
MyFunction2 <- function(x, z) {
a <- 10
x <- x + 1
print(x)
}
Run Code Online (Sandbox Code Playgroud)
这是输出:
critic("source.R")
# MyFunction: no visible binding for global variable ‘y’
# MyFunction2: local variable ‘a’ assigned but may not be used
# MyFunction2: parameter ‘x’ changed by assignment
# MyFunction2: parameter ‘z’ may not be used
Run Code Online (Sandbox Code Playgroud)