在我自己的包中使用data.table包 [英] Using data.table package inside my own package
问题描述
我想在我自己的包中使用data.table包。 MWE如下:
I am trying to use the data.table package inside my own package. MWE is as follows:
我创建一个函数test.fun,它只是创建一个小的data.table对象,然后将Val A列。代码
I create a function, test.fun, that simply creates a small data.table object, and then sums the "Val" column grouping by the "A" column. The code is
test.fun<-function ()
{
library(data.table)
testdata<-data.table(A=rep(seq(1,5), 5), Val=rnorm(25))
setkey(testdata, A)
res<-testdata[,{list(Ct=length(Val),Total=sum(Val),Avg=mean(Val))},"A"]
return(res)
}
在常规R会话中创建此函数,然后运行该函数时,它会按预期工作。
When I create this function in a regular R session, and then run the function, it works as expected.
> res<-test.fun()
data.table 1.8.0 For help type: help("data.table")
> res
A Ct Total Avg
[1,] 1 5 -0.5326444 -0.1065289
[2,] 2 5 -4.0832062 -0.8166412
[3,] 3 5 0.9458251 0.1891650
[4,] 4 5 2.0474791 0.4094958
[5,] 5 5 2.3609443 0.4721889
当我把这个函数放入一个包,安装包,加载包,然后运行该函数,我得到一个错误消息。
When I put this function into a package, install the package, load the package, and then run the function, I get an error message.
> library(testpackage)
> res<-test.fun()
data.table 1.8.0 For help type: help("data.table")
Error in `[.data.frame`(x, i, j) : object 'Val' not found
任何人都可以向我解释为什么会发生这种情况,要解决这个问题。任何帮助是非常感激。
Can anybody explain to me why this is happening and what I can do to fix it. Any help is very much appreciated.
推荐答案
Andrie的猜测是对的,+1。有一个常见问题:
Andrie's guess is right, +1. There is a FAQ on it :
常见问题6.9:我创建了一个依赖于data.table的包。我如何
确保我的包是data.table意识,以便从
data.frame的继承工作?
DESCRIPTION档案 css css cs >字段 或 导入:
字段。在这两种情况下,始终 将 import(data.table)
添加到NAMESPACE文件。有关详细信息,请参阅包依赖关系 noreferrer>编写R扩展手册。
In your DESCRIPTION file include data.table
in either the Depends:
field or the Imports:
field. In both cases, always add import(data.table)
to your NAMESPACE file. For more details read Package Dependencies section in the Writing R Extensions manual.
更多背景...在 [.data.table
(和其他 data.table
函数),您将看到一个切换取决于调用 cedta()
。这表示调用环境数据表意识。键入 data.table ::: cedta
揭示它是如何完成的。它依赖于调用包具有命名空间,并且该命名空间导入或依赖于 data.table
。这是如何将 data.table
传递给非 data.table-aware 包(例如 base
),这些包可以在 data.table $ c $上使用绝对标准的
[。data.frame
c>,完全不知道 data.frame
是()
a data.table
。
Further background ... at the top of [.data.table
(and other data.table
functions), you'll see a switch depending on the result of a call to cedta()
. This stands for Calling Environment Data Table Aware. Typing data.table:::cedta
reveals how it's done. It relies on the calling package having a namespace, and, that namespace Import'ing or Depend'ing on data.table
. This is how data.table
can be passed to non-data.table-aware packages (such as functions in base
) and those packages can use absolutely standard [.data.frame
syntax on the data.table
, blissfully unaware that the data.frame
is()
a data.table
, too.
这也是为什么 data.table
与无命名空间包兼容,为什么在用户请求时,我们不得不要求这样的包的作者添加一个命名空间到它们的包是兼容的。幸运的是,现在R为缺少一个(从v2.14.0)的软件包添加了默认命名空间,这个问题已经消失:
This is also why data.table
inheritance didn't used to be compatible with namespaceless packages, and why upon user request we had to ask authors of such packages to add a namespace to their package to be compatible. Happily, now that R adds a default namespace for packages missing one (from v2.14.0), that problem has gone away :
更改R VERSION 2.14.0
*所有软件包都必须有一个命名空间,如果没有在源代码中提供,则会在安装时创建一个。
CHANGES IN R VERSION 2.14.0
* All packages must have a namespace, and one is created on installation if not supplied in the sources.
这篇关于在我自己的包中使用data.table包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!