问题描述
我必须制作一个依赖于包 data.table 的R包。
randomdt< - function(){
dt< - data.table(random = rnorm(10))
dt [dt $ random> 0]
}
[对于data.frame而不是data.table,因此错误
错误在`[.data.frame` ):未选定的列
通常这将通过使用 get('[。data.table')
或类似的方法( package :: function
是最简单的),但似乎不工作。毕竟, [
是一个原始函数,我不知道它的方法如何工作。
那么,如何从我的包中调用data.table [
$ b
根据的一些反馈更新和和。
大致来说,您可能会考虑两种方法。第一种方法是将依赖性构建到软件包中,第二种方法是在R代码中包含用于测试 data.table
(甚至可以自动安装)的行如果没有找到)。
data.table
在6.9中特别提到了这一点,并声明您可以确保<$ c $
如注释中所述,这是许多软件包中常见的R行为。
另一种方法是创建特定行的代码,测试和导入所需的包作为您的代码的一部分。考虑到使用上面提供的选项的优雅,这是,我认为,不是理想的解决方案。
一个简单的方法是使用 require
或 library
以检查是否存在 data.table
,如果无法附加,则抛出错误。你甚至可以使用一组简单的条件语句来运行 install.packages
来安装所需的,如果加载它们失败。
库
和 require
,并强制使用库
在包裹对于即将到来的代码绝对必要的情况下。 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]
}
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
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.
So, how can I call the data.table [
function from my package?
Updated based on some feedback from MichaelChirico and comments by Arun and 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).
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:
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.
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 (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的包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!