我们可以有更多的错误(消息)吗? [英] Can we have more error (messages)?

查看:105
本文介绍了我们可以有更多的错误(消息)吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果函数使用变量,在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屋!

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