[英]Making a package in R that depends on data.table
I have to make an R package that depends on the package data.table . 我必须制作一个依赖于包data.table的R包。 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]
}
the function [ will use the method for data.frame not for data.table and therefore the error 函数[将对data.frame使用该方法,而不对data.table使用该方法,因此错误
Error in `[.data.frame`(x, i) : undefined columns selected
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. 通常,这可以通过使用
get('[.data.table')
或类似的方法( package::function
是最简单的)来解决,但似乎不起作用。 After all, [
is a primitive function and I don't know how the methods to it work. 毕竟,
[
是一个原始函数,我不知道该函数的工作方式。
So, how can I call the data.table [
function from my package? 那么,如何从包中调用data.table
[
函数?
Updated based on some feedback from MichaelChirico and comments by Arun and Soheil . 根据MichaelChirico的一些反馈以及Arun和Soheil的评论进行了更新 。
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). 第一种是将依赖关系构建到包本身中,第二种是在R代码中包括用于测试
data.table
是否存在的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: data.table
FAQ在6.9中专门解决了这一问题 ,并指出您可以通过以下方式确保您的软件包正确加载了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.
i)在您的Description文件的Depends:字段中包含data.table,或者ii)在您的DESCRIPTION文件的Imports:字段中包含data.table,在NAMESPACE文件中的import(data.table)包含。
As noted in the comments, this is common R behavior that is in numerous packages. 如注释中所述,这是许多程序包中的常见R行为。
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.
但是,在技术上是可行的。
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. 一种简单的方法是使用
require
或library
检查data.table
的存在,如果无法附加错误,则会抛出错误。 You could even use a simple set of conditional statements to run install.packages
to install what you need if loading them fails. 您甚至可以使用一组简单的条件语句来运行
install.packages
以在加载失败时安装所需的内容。
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. 艺辉谢(的knitr名望)有大约之间的差别很大交
library
,并require
在这里并为只使用一个强大的情况下, library
在包是为即将到来的代码绝对必要的情况下。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.