在编写自己的R包时,我似乎无法正确导入其他包 [英] When writing my own R package, I can't seem to get other packages to import correctly

查看:217
本文介绍了在编写自己的R包时,我似乎无法正确导入其他包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,第一次尝试写一个R包,我被卡住了。以下是我创建包的方法:

  package.skeleton(pkg,code_files = some.filenames)
roxygenize (okg)

我正在使用roxygen2并在我的pkg-package中使用以下导入.R文件:

  @import data.table zoo lubridate 

从终端开始,我运行:

  R CMD build pkg 
R CMD check pkg
R CMD install pkg

在检查阶段,我得到以下警告:


**准备延迟加载包

警告:替换先前的导入'小时'加载'lubridate时'

警告:加载'lubridate'时替换先前导入'mday'


警告:加载'lubridate'时替换上一个导入'月'
b $ b警告:在加载'lubridate'时替换先前的导入'wday'

警告:替换pr加载'lubridate'时导致'周''
警告:加载'lubridate'时替换先前导入'yday'

警告:加载'lubridate'时替换上一个导入'year'

**帮助

* 安装帮助索引

**构建包索引...

**测试是否可以加载已安装的软件包


警告消息:

1:在加载'lubridate'时替换先前的导入'小时'

2:替换之前的加载'lubridate'时导入'mday'


3:加载'lubridate'时替换上一个导入'月'


4:加载'lubridate'时替换先前导入'wday'< br>
5:在加载'lubridate'时替换先前的导入'周'

6:在加载'lubridate'时替换先前的导入'yday'


7:替换之前的加载'lubridate'时导入'年'




我真的不确定如何制作这些,但它们似乎是覆盖命名空间中的东西的典型警告。无论如何,我可以安装软件包,但是当我尝试使用它时会发生什么:



library(pkg)

覆盖+和 - POSIXt,日期和差异时间的方法

警告信息:

1:替换上一次导入'小时'时加载'lubridate'

2:在加载'lubridate'时替换先前导入'mday'


3:在加载'lubridate'时替换先前的导入'月'

4:在加载'lubridate'时替换先前导入'wday'


5:在加载'lubridate'时替换先前导入'周'

6:替换先前导入'yday'时加载'lubridate'

7:加载'lubridate'时替换上一个导入'年'


d< - my.function(arg1,arg2)

错误在MATCH(x,x):找不到函数MATCH



使用traceback(),我发现这是在调用merge.zoo()期间生成的。所以我尝试在我的R会话期间手动加载动物园,然后功能正常,没有错误消息。



我试过用手改变进口的顺序在pkg-package.R文件中,以及在NAMESPACE中。根据我在其他地方找到的东西,我没有添加任何Imports或Depends来描述。帮助?

解决方案

警告是因为data.table和lubridate都定义了符号小时等;参见 data.table :: hour lubridate :: hour 。你可以通过只导入你想要的lubridate / data.table中的函数来避免这种情况,而不是整个包;标准的NAMESPACE文件将包含

  importFrom(lubridate,hour)


。在roxygen2中你会使用标签:

  @importFrom lubridate hour 

MATCH问题可能是因为 merge 调度错误,可能是因为zoo应该在其名称空间 S3method(merge,zoo)而不是 export(merge.zoo),如编写R Extensions,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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆