在 R 中制作一个依赖于 data.table 的包 [英] Making a package in R that depends on data.table
问题描述
我必须制作一个依赖包data.table的R包.但是,如果我要做一个功能,例如包中的下一个功能
I have to make an R package that depends on the package data.table. However, if I would do a function such as the next one in the package
randomdt <- function(){
dt <- data.table(random = rnorm(10))
dt[dt$random > 0]
}
函数 [ 将使用 data.frame 的方法而不是 data.table 的方法,因此会出错
the function [ will use the method for data.frame not for data.table and therefore the error
Error in `[.data.frame`(x, i) : undefined columns selected
会出现.通常这可以通过使用 get('[.data.table')
或类似方法(package::function
是最简单的)来解决,但这似乎不起作用.毕竟, [
是一个原始函数,我不知道它的方法是如何工作的.
will appear. Usually this would be solved by using get('[.data.table')
or similar method (package::function
is the simplest) but that appears not to work. After all, [
is a primitive function and I don't know how the methods to it work.
那么,如何从我的包中调用 data.table [
函数?
So, how can I call the data.table [
function from my package?
推荐答案
根据 MichaelChirico 的一些反馈和评论进行了更新由 Arun 和 Soheil一个>.
Updated based on some feedback from MichaelChirico and comments by Arun and Soheil.
粗略地说,您可以考虑两种方法.第一个是将依赖项构建到您的包本身中,而第二个是在您的 R 代码中包含测试 data.table
存在的行(如果找不到,甚至可能自动安装它).
Roughly speaking, there's two approaches you might consider. The first is building the dependency into your package itself, while the second is including lines in your R code that test for the presence of data.table
(and possibly even install it automatically if it is not found).
data.table
FAQ 在 6.9 中专门解决了这个问题,并声明您可以确保 data.table
由您的包正确加载:
The data.table
FAQ specifically addresses this in 6.9, and states that you can ensure that data.table
is appropriately loaded by your package by:
i) 在您的说明文件的 Depends: 字段中包含 data.table,或 ii) 在您的说明文件的 Imports: 字段中包含 data.table 并在您的 NAMESPACE 文件中的 import(data.table).
Either i) include data.table in the Depends: field of your DESCRIPTION file, or ii) include data.table in the Imports: field of your DESCRIPTION file AND import(data.table) in your NAMESPACE file.
如评论中所述,这是许多包中常见的 R 行为.
As noted in the comments, this is common R behavior that is in numerous packages.
另一种方法是创建特定的代码行来测试和导入所需的包作为代码的一部分.鉴于使用上面提供的选项的优雅,我认为这不是理想的解决方案.不过,这在技术上是可行的.
An alternative approach is to create specific lines of code which test for and import the required packages as part of your code. This is, I would contend, not the ideal solution given the elegance of using the option provided above. However, it is technically possible.
一个简单的方法是使用 require
或 library
来检查 data.table
的存在,并使用如果无法附加,则会引发错误.如果加载失败,您甚至可以使用一组简单的条件语句来运行 install.packages
来安装所需的内容.
A simple way of doing this would be to use either require
or library
to check for the existence of data.table
, with an error thrown if it could not be attached. You could even use a simple set of conditional statements to run install.packages
to install what you need if loading them fails.
Yihui Xie(knitr 成名)有一篇很棒的帖子,介绍了 library
和 require
之间的区别 here 并为仅使用 library
的情况提供了强有力的理由代码.
Yihui Xie (of knitr fame) has a great post about the difference between library
and require
here and makes a strong case for just using library
in cases where the package is absolutely essential for the upcoming code.
这篇关于在 R 中制作一个依赖于 data.table 的包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!