简体   繁体   English

在编写自己的R包时,我似乎无法正确导入其他包

[英]When writing my own R package, I can't seem to get other packages to import correctly

Alright, first attempt at writing an R package and I'm stuck. 好吧,第一次尝试写一个R包,我被卡住了。 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: 我正在使用roxygen2并在我的“pkg-package.R”文件中输入以下内容:

@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' 警告:加载'lubridate'时替换上一个导入'小时'
Warning: replacing previous import 'mday' when loading 'lubridate' 警告:在加载'lubridate'时替换先前的导入'mday'
Warning: replacing previous import 'month' when loading 'lubridate' 警告:加载'lubridate'时替换上一个导入'月'
Warning: replacing previous import 'wday' when loading 'lubridate' 警告:在加载'lubridate'时替换先前的导入'wday'
Warning: replacing previous import 'week' when loading 'lubridate' 警告:加载'lubridate'时替换上一个导入'周'
Warning: replacing previous import 'yday' when loading 'lubridate' 警告:在加载'lubridate'时替换先前的导入'yday'
Warning: replacing previous import 'year' when loading 'lubridate' 警告:加载'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' 1:加载'lubridate'时替换先前的导入'小时'
2: replacing previous import 'mday' when loading 'lubridate' 2:加载'lubridate'时替换先前的导入'mday'
3: replacing previous import 'month' when loading 'lubridate' 3:加载'lubridate'时替换上一个导入'月'
4: replacing previous import 'wday' when loading 'lubridate' 4:加载'lubridate'时替换先前的导入'wday'
5: replacing previous import 'week' when loading 'lubridate' 5:加载'lubridate'时替换上一个导入'周'
6: replacing previous import 'yday' when loading 'lubridate' 6:加载'lubridate'时替换先前的导入'yday'
7: replacing previous import 'year' when loading 'lubridate' 7:加载'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) 库(PKG)
Overriding + and - methods for POSIXt, Date and difftime 覆盖POSIXt,Date和difftime的+和 - 方法
Warning messages: 警告信息:
1: replacing previous import 'hour' when loading 'lubridate' 1:加载'lubridate'时替换先前的导入'小时'
2: replacing previous import 'mday' when loading 'lubridate' 2:加载'lubridate'时替换先前的导入'mday'
3: replacing previous import 'month' when loading 'lubridate' 3:加载'lubridate'时替换上一个导入'月'
4: replacing previous import 'wday' when loading 'lubridate' 4:加载'lubridate'时替换先前的导入'wday'
5: replacing previous import 'week' when loading 'lubridate' 5:加载'lubridate'时替换上一个导入'周'
6: replacing previous import 'yday' when loading 'lubridate' 6:加载'lubridate'时替换先前的导入'yday'
7: replacing previous import 'year' when loading 'lubridate' 7:加载'lubridate'时替换上一个导入'年'
d <- my.function(arg1, arg2) d < - my.function(arg1,arg2)
Error in MATCH(x, x) : could not find function "MATCH" MATCH(x,x)出错:找不到函数“MATCH”

Using traceback(), I found out that this is being generating during a call to merge.zoo(). 使用traceback(),我发现这是在调用merge.zoo()期间生成的。 So I tried loading zoo by hand during my R session and voila, then the function works correctly without the error message. 所以我尝试在我的R会话期间手动加载动物园,然后功能正常,没有错误消息。

I have tried changing the ordering of the imports by hand in both the "pkg-package.R" file, as well as in NAMESPACE. 我已经尝试在“pkg-package.R”文件和NAMESPACE中手动更改导入的顺序。 Based on something I found elsewhere, I have not added any Imports or Depends to DESCRIPTION, however. 根据我在其他地方找到的东西,我没有添加任何Imports或Depends来描述。 Help? 救命?

The warnings are because data.table and lubridate both define a symbol hour , etc; 警告是因为data.table和lubridate都定义了一个符号hour等; see data.table::hour and lubridate::hour . 请参阅data.table::hourlubridate::hour You could avoid this by importing just the functions from lubridate / data.table that you want, rather than the whole package; 你可以通过只导入你想要的lubridate / data.table中的函数来避免这种情况,而不是整个包; a standard NAMESPACE file would contain 标准的NAMESPACE文件将包含

importFrom(lubridate, hour)

for instance. 例如。 In roxygen2 you would use the tag: 在roxygen2中你会使用标签:

@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. MATCH问题可能是因为merge调度错误,可能是因为zoo应该在其名称空间S3method(merge, zoo)而不是export(merge.zoo) ,如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...). 这里的解决方案是联系zoo的维护者, packageDescription('zoo')$Maintainer (维护人员对R非常熟悉,我觉得我错误地诊断了......)。

作为MATCH错误的临时解决方法,我已经成功列出了包的DESCRIPTION文件的Depends:部分下的zoo包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 编写R包:如何导入另一个包? - Writing R packages: how to import another package? R包:当导出的函数没有显式调用其他包中的函数时,“导入”如何工作,但子例程却如此 - R Package: how “import” works when my exported function does not call explicitly a function from other packages, but a subroutine does 编写 R package 时如何导入“%&gt;%”? - How do I import “%>%” when writing an R package? 我似乎无法在 R studio for windows 上安装“ecospat”包 - I can't seem to install the 'ecospat' package on R studio for windows R包 - 我应该导入`methods`包吗? - R packages - should I import the `methods` package? 在R中,如何自动安装使用我自己的包的包? - In R, how to install packages used my own package automatically? 如何在我自己的R包中包含一个需要的包? - How include a requried packages inside my own package in R? 不要在 R 中暴露自己的 package 中导入包的功能 - Don't expose functions from imported packages in own package in R R不会加载已安装的软件包,理由是错误:必须先卸载其他软件包(并且不能卸载它们) - R won't load installed package, citing errors that other packages must be unloaded first (and that they can't be unloaded) R包:当我库我的包时如何让我的包加载其他包 - R package: how to make my package loading other package when I library my package
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM