简体   繁体   English

编写R包:如何导入另一个包?

[英]Writing R packages: how to import another package?

As a minimal working example, I'm trying to import some objects from the MASS package into my own package (called Test ) - take the abbey dataset for example: 作为最小的工作例子中,我试图从一些物体导入MASS包到我自己的包(称为Test ) -采取abbey数据集,例如:

### In R/Test.R:
#' @import MASS
abbey     # Check that the dataset has been imported OK

### DESCRIPTION:
Package: Test
...
Imports: MASS

### NAMESPACE:
# Generated by roxygen2 (4.0.1): do not edit by hand
import(MASS)

I hit Build & Reload in RStudio and get the error: 我在RStudio中点击Build & Reload并得到错误:

==> devtools::document(roclets=c('rd', 'collate', 'namespace'))

Updating Test documentation
Loading Test
Error in eval(expr, envir, enclos) : object 'abbey' not found
Writing NAMESPACE
Documentation completed

==> Rcmd.exe INSTALL --no-multiarch --with-keep.source Test

* installing to library '.../R/R-3.1.0/library'
* installing *source* package 'Test' ...
** R
** preparing package for lazy loading
Error in eval(expr, envir, enclos) : object 'abbey' not found
Error : unable to load R code in package 'Test'
ERROR: lazy loading failed for package 'Test'
* removing '.../R/R-3.1.0/library/Test'
* restoring previous '.../R/R-3.1.0/library/Test'

Exited with status 1.

It seems that even the most basic import has failed - the system can't find abbey . 似乎即使是最基本的import也失败了 - 系统找不到abbey Clearly I must be overlooking something obvious - what's going wrong? 显然,我必须忽视一些明显的东西 - 出了什么问题?

From what I tried: You can not import abbey because it is not exported by MASS. 从我尝试过:你不能导入修道院,因为它不是由MASS导出的。

> library(MASS)
> 'abbey' %in% getNamespaceExports(getNamespace('MASS'))
[1] FALSE

abbey is a package dataset, defined by a data/abbey.rda file, not a regular symbol name defined by a package. abbey是一个包数据集,由data / abbey.rda文件定义,而不是由包定义的常规符号名称。

As suggested you should just use MASS::abbey, or even add in R/data.R: 建议您只使用MASS :: abbey,甚至添加R / data.R:

abbey <- MASS::abbey

to copy the dataset in your package namespace at install time. 在安装时复制软件包命名空间中的数据集。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM