在编写自己的 R 包时,我似乎无法正确导入其他包 [英] When writing my own R package, I can't seem to get other packages to import correctly
问题描述
好的,第一次尝试编写 R 包,我被卡住了.这是我创建包的方式:
package.skeleton("pkg",code_files=some.filenames)roxygenize(okg")
我正在使用 roxygen2 并且在我的pkg-package.R"文件中有以下导入:
@import data.table zoo lubridate
然后从终端运行:
R CMD 构建包R CMD 检查包R CMD 安装包
在检查阶段,我收到以下警告:
<块引用>** 为延迟加载准备包
警告:加载lubridate"时替换先前导入的小时"
警告:加载‘lubridate’时替换之前的导入‘mday’
警告:加载‘lubridate’时替换之前的导入‘month’
警告:加载‘lubridate’时替换之前的导入‘wday’
警告:加载‘lubridate’时替换上一个导入‘week’
警告:加载‘lubridate’时替换之前的导入‘yday’
警告:加载lubridate"时替换以前的导入年份"
** 帮助
* 安装帮助索引
** 构建包索引...
** 测试是否可以加载已安装的包
警告信息:
1:在加载‘lubridate’时替换之前导入的‘hour’
2:在加载‘lubridate’时替换之前的导入‘mday’
3: 加载‘lubridate’时替换之前导入的‘month’
4:在加载‘lubridate’时替换之前的导入‘wday’
5:在加载‘lubridate’时替换之前导入的‘week’
6:在加载‘lubridate’时替换之前的导入‘yday’
7:加载‘lubridate’时替换之前导入的‘year’
我真的不知道该怎么做,但它们看起来像是覆盖命名空间中的东西的典型警告.在任何情况下,我都可以安装该软件包,但是当我尝试使用它时会发生以下情况:
<块引用><块引用>库(pkg)
覆盖 POSIXt、Date 和 difftime 的 + 和 - 方法
警告信息:
1:在加载‘lubridate’时替换之前导入的‘hour’
2:在加载‘lubridate’时替换之前的导入‘mday’
3: 加载‘lubridate’时替换之前导入的‘month’
4:在加载‘lubridate’时替换之前的导入‘wday’
5:在加载‘lubridate’时替换之前导入的‘week’
6:在加载‘lubridate’时替换之前的导入‘yday’
7:在加载‘lubridate’时替换之前导入的‘year’
d <- my.function(arg1, arg2)
MATCH(x, x) 中的错误:找不到函数MATCH"
使用 traceback(),我发现这是在调用 merge.zoo() 期间生成的.所以我尝试在 R 会话期间手动加载 zoo,瞧,然后该功能正常运行,没有错误消息.
我已经尝试在pkg-package.R"文件和 NAMESPACE 中手动更改导入的顺序.然而,根据我在其他地方找到的内容,我没有在描述中添加任何导入或依赖.有帮助吗?
警告是因为data.table和lubridate都定义了一个符号hour
等;参见 data.table::hour
和 lubridate::hour
.您可以通过仅从 lubridate/data.table 导入您想要的函数而不是整个包来避免这种情况;标准 NAMESPACE 文件将包含
importFrom(lubridate, 小时)
例如.在 roxygen2 中,您将使用标签:
@importFrom 润滑小时
MATCH 问题可能是因为 merge
的调度不正确,可能是因为 zoo 应该在其名称空间中使用 S3method(merge, zoo)
而不是 export(merge.zoo)
,如编写 R 扩展,1.6.2 中所述.这里的解决方案是联系 zoo
的维护者,packageDescription('zoo')$Maintainer
(维护者对 R 非常精通,我觉得我错了-确诊...).
Alright, first attempt at writing an R package and I'm stuck. Here's how I create the package:
package.skeleton("pkg",code_files=some.filenames)
roxygenize("okg")
I'm using roxygen2 and have the following imports in my "pkg-package.R" file:
@import data.table zoo lubridate
From a terminal, I then run:
R CMD build pkg
R CMD check pkg
R CMD install pkg
During the check phase, I get the following warnings:
** preparing package for lazy loading
Warning: replacing previous import ‘hour’ when loading ‘lubridate’
Warning: replacing previous import ‘mday’ when loading ‘lubridate’
Warning: replacing previous import ‘month’ when loading ‘lubridate’
Warning: replacing previous import ‘wday’ when loading ‘lubridate’
Warning: replacing previous import ‘week’ when loading ‘lubridate'
Warning: replacing previous import ‘yday’ when loading ‘lubridate’
Warning: replacing previous import ‘year’ when loading ‘lubridate’
** help
* installing help indices
** building package indices ...
** testing if installed package can be loaded
Warning messages:
1: replacing previous import ‘hour’ when loading ‘lubridate’
2: replacing previous import ‘mday’ when loading ‘lubridate’
3: replacing previous import ‘month’ when loading ‘lubridate’
4: replacing previous import ‘wday’ when loading ‘lubridate’
5: replacing previous import ‘week’ when loading ‘lubridate’
6: replacing previous import ‘yday’ when loading ‘lubridate’
7: replacing previous import ‘year’ when loading ‘lubridate’
I'm really not sure what to make of those, but they seem like typical warnings from overwriting stuff in namespace. In any case, I am able to install the package, but here's what happens when I try to use it:
library(pkg)
Overriding + and - methods for POSIXt, Date and difftime
Warning messages:
1: replacing previous import ‘hour’ when loading ‘lubridate’
2: replacing previous import ‘mday’ when loading ‘lubridate’
3: replacing previous import ‘month’ when loading ‘lubridate’
4: replacing previous import ‘wday’ when loading ‘lubridate’
5: replacing previous import ‘week’ when loading ‘lubridate’
6: replacing previous import ‘yday’ when loading ‘lubridate’
7: replacing previous import ‘year’ when loading ‘lubridate’
d <- my.function(arg1, arg2)
Error in MATCH(x, x) : could not find function "MATCH"
Using traceback(), I found out that this is being generating during a call to merge.zoo(). So I tried loading zoo by hand during my R session and voila, then the function works correctly without the error message.
I have tried changing the ordering of the imports by hand in both the "pkg-package.R" file, as well as in NAMESPACE. Based on something I found elsewhere, I have not added any Imports or Depends to DESCRIPTION, however. Help?
The warnings are because data.table and lubridate both define a symbol hour
, etc; see data.table::hour
and lubridate::hour
. You could avoid this by importing just the functions from lubridate / data.table that you want, rather than the whole package; a standard NAMESPACE file would contain
importFrom(lubridate, hour)
for instance. In roxygen2 you would use the tag:
@importFrom lubridate hour
The MATCH problem is probably because merge
is dispatching incorrectly, probably because zoo should have in its name space S3method(merge, zoo)
rather than export(merge.zoo)
, as described in Writing R Extensions, 1.6.2. The solution here is to contact the maintainer of zoo
, packageDescription('zoo')$Maintainer
(the maintainer is sufficiently versed in R that I feel like I've mis-diagnosed...).
这篇关于在编写自己的 R 包时,我似乎无法正确导入其他包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!