我们可以有更多的错误(消息)吗? [英] Can we have more error (messages)?
问题描述
如果函数使用变量,在R中是否有一种弹出错误消息的方法 没有在函数主体中声明:即,我希望有人标记这种类型的函数
Is there a way, in R, to pop up an error message if a function uses a variable not declared in the body of the function: i.e, i want someone to flag this type of functions
aha<-function(p){
return(p+n)
}
参见;如果某个地方碰巧有一个"n"变量,那么aha(p = 2)会给我一个答案",因为R只会从那个叫做环境"的神秘地方取"n"
see; if there happens to be a "n" variable lying somewhere, aha(p=2) will give me an "answer" since R will just take "n" from that mysterious place called the "environment"
推荐答案
如果您想在代码编写阶段而不是在运行时发现此类潜在问题,那么codetools
包是您的朋友.>
If you want to detect such potential problems during the code-writing phase and not during run-time, then the codetools
package is your friend.
library(codetools)
aha<-function(p){
return(p+n)
}
#check a specific function:
checkUsage(aha)
#check all loaded functions:
checkUsageEnv(.GlobalEnv)
这些将告诉您no visible binding for global variable ‘n’
.
这篇关于我们可以有更多的错误(消息)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!