英文:
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 <- 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 自己的 with
和 within
),但这会让检查全局符号的解析器感到困惑,因为它看到了未引用的内容... 并(错误地)推断出一个全局符号。
我们无法修复该解析器,但您可以将其静音。要做到这一点,您可以依赖基本的 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("errorMessage"))
in any of your R files, or just do
errorMessage <- NULL
somewhere to create a ('fake' as in unused) identifier.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论