使用数据表在包开发中:未定义的全局函数或变量

huangapple go评论56阅读模式
英文:

using data.table in package development: Undefined global functions or variables

问题

以下代码在我正在开发的包中创建了R CMD check中的警告:

Results[, errorMessage := as.character(errorMessage)]

no visible binding for global variable ‘errorMessage’
   Undefined global functions or variables:
     errorMessage

Results$errorMessage <- as.character(Results$errorMessage) 是正常的。

有人知道为什么会发生这种情况以及我应该如何更改data.table的代码以避免错误吗?

英文:

The following code in a package I'm developing creates warning in R CMD check

Results[, errorMessage := as.character(errorMessage)]

no visible binding for global variable ‘errorMessage’
   Undefined global functions or variables:
     errorMessage

but Results$errorMessage &lt;- as.character(Results$errorMessage) is fine

Does anyone know why this happens and how should I change the code with data.table to avoid the error?

答案1

得分: 4

Here is the translated content:

这是一个已知问题,很可能有文档记录(尽管我刚刚在“FAQ”中未能找到...)。 data.table 的一个神奇之处在于,在您使用它的对象的范围内,不需要引用标识符(类似于 R 自己的 withwithin),但这会让检查全局符号的解析器感到困惑,因为它看到了未引用的内容... 并(错误地)推断出一个全局符号。

我们无法修复该解析器,但您可以将其静音。要做到这一点,您可以依赖基本的 R 并在任何您的 R 文件中添加以下内容:

utils::globalVariables(c("errorMessage"))

或者只需执行以下操作:

errorMessage <- NULL

在某处创建一个('fake',即未使用的)标识符。

英文:

That it is a known issue, and likely documented (though I just failed to find it in the FAQ ...). One of the magic powers of data.table is to not require quoted identifiers within the scope of the object you use it with (similar to R's own with and within) but that confused the parser checking for global symbols as it sees something unquoted ... and (wrongly) infers a global symbol.

We cannot fix that parser, but you can silence it. To do so, you can either rely on base R and put a

utils::globalVariables(c(&quot;errorMessage&quot;)) 

in any of your R files, or just do

errorMessage &lt;- NULL

somewhere to create a ('fake' as in unused) identifier.

huangapple
  • 本文由 发表于 2023年5月13日 21:17:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76242926.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定