我们可以有更多错误(消息)吗?

发布于 2024-08-19 09:33:04 字数 198 浏览 8 评论 0原文

在 R 中,如果函数使用变量,有没有办法弹出错误消息 未在函数体内声明:即,我希望有人标记这种类型的函数,

aha<-function(p){
  return(p+n)
}

请参阅;如果某个地方碰巧有一个“n”变量,aha(p=2) 会给我一个“答案”,因为 R 只会从那个称为“环境”的神秘地方获取“n”

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)
}

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"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

野鹿林 2024-08-26 09:33:04

如果您想在代码编写阶段而不是运行时检测此类潜在问题,那么 codetools 包就是您的朋友。

library(codetools)
aha<-function(p){ 
  return(p+n) 
}

#check a specific function:
checkUsage(aha) 

#check all loaded functions:
checkUsageEnv(.GlobalEnv)

这些将告诉您全局变量“n”没有可见的绑定

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)

These will tell you that no visible binding for global variable ‘n’.

泛泛之交 2024-08-26 09:33:04

里奇的建议非常好。

我只想补充一点,您应该考虑创建在干净的 R 环境中运行的单元测试用例。这也将消除对全局变量的担忧,并确保您的函数按其应有的方式运行。您可能需要考虑使用 RUnit 来实现此目的。我计划每天晚上使用 RScript 在新环境中运行我的测试套件,这非常有效,可以捕获任何类型的范围问题等。

Richie's suggestion is very good.

I would just add that you should consider creating unit test cases that would run in a clean R environment. That will also eliminate the concern about global variables and ensures that your functions behave the way that they should. You might want to consider using RUnit for this. I have my test suite scheduled to run every night in a new environment using RScript, and that's very effective and catching any kind of scope issues, etc.

已下线请稍等 2024-08-26 09:33:04

编写 R 代码来检查其他 R 代码将会很棘手。您必须找到一种方法来确定哪些代码位是变量声明,然后尝试确定它们是否已在函数中声明。
编辑:前面的陈述是正确的,但正如 Aniko 指出的那样,艰苦的工作已经在 codetools 包。

一件可能对您有用的相关事情是强制从函数本身(而不是从封闭环境)获取变量。

由于未声明 n,因此函数的修改版本始终会失败。

aha <- function(p) 
{ 
   n <- get("n", inherits=FALSE)
   return(p+n)
}

Writing R codes to check other R code is going to be tricky. You'd have to find a way to determine which bits of code were variable declarations, and then try and work out whether they'd already been declared within the function.
EDIT: The previous statement would have been true, but as Aniko pointed out, the hard work has already been done in the codetools package.

One related thing that may be useful to you is to force a variable to be taken from the function itself (rather than from an enclosing environment).

This modified version of your function will always fail, since n is not declared.

aha <- function(p) 
{ 
   n <- get("n", inherits=FALSE)
   return(p+n)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文