R data.table':='在直接调用中工作,但在包中的相同功能失败 [英] R data.table ':=' works in direct call, but same function in a package fails
问题描述
使用R的data.table包,
Using R's data.table package,
这适用于:
instruction = "a = data.table(name=1:3, value=1:3, blah=1:3); a[,c('value', 'blah'):=NULL]"
eval(parse(text=instruction))
# name
#1: 1
#2: 2
#3: 3
这适用于:
myFunc = function(instruction) {
eval(parse(text=instruction))
}
myFunc(instruction)
# name
#1: 1
#2: 2
#3: 3
现在,将此函数放入一个包, ,并尝试调用它。这不工作:
Now, put this function into a package, load it, and try to call it. This doesn't work:
myFuncInPackage(instruction)
#Error in `:=`(c("value", "blah"), NULL) :
# Check that is.data.table(DT) == TRUE. Otherwise, := and `:=`(...) are defined for use in j, once only and in particular ways. See help(":=").
为什么?
编辑:@Roland指出,在包中添加data.table 依赖
字段使其工作。但是,我不认为这是一个伟大的解决方案,因为包实际上不依赖,需要或使用data.table。我只是想要能够使用data.table与包。
@Roland points out that adding data.table in the package Depends
field makes it work. However, I don't think this is a great solution because the package doesn't really depend on, require, or use data.table. I just want to be able to use data.table with the package.
此外,其他一切与data.table工作正常的函数,只是不是:=
运算符。
In addition, everything else with data.table works fine in the function, just not the :=
operator.
所以我想一个后续问题可能是:我应该添加data.table包我写,使data.tables工作作为预期在该包的功能?
So I guess a followup question could be: should I add data.table to the Depends of every package I write, so that data.tables work as expected within functions of that package? This doesn't seem right... what is the correct way to approach this?
推荐答案
我有同样的问题,我解决了它将 data.table
添加到导入
和取决于
。我的 data.table
版本是 1.9.6
I had same problem and I solved it adding data.table
to Imports
and Depends:
. My data.table
version is 1.9.6
这篇关于R data.table':='在直接调用中工作,但在包中的相同功能失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!