2017-02-22 63 views
1

我有一个包(称为testpackage1),其中包含一个名为readData()的方法。如何从另一个包中导入数据

该方法读取放置在testpackage1的数据文件夹中的test.data.rda文件,并在某些操作之后返回数据帧。

这是testpackage1唯一R档:

#' Reads data and transforms it 
#' 
#' @return a data.frame 
#' @export 
#' 
#' @examples my.df <- readData() 
readData <- function() { 
    return(subset(test.data, x < 50)) 
} 

initPackage <- function() { 
    test.data <- data.frame(x = seq(1, 100), 
          y = seq(101, 200)) 
    devtools::use_data(test.data, overwrite = TRUE) 
} 

调用initPackage方法创建的数据帧,并将其保存在数据文件夹中的文件.rda。

现在我已经创建了一个名为testpackage2第二包,也只有一个R档:

#' Gets the data 
#' 
#' @import testpackage1 
#' @export 
#' 
#' @examples hello() 
hello <- function() { 
    print(testpackage1::readData()) 
} 

我建了两个包,然后开始一个新的R对话和类型:

> library(testpackage2) 
> hello() 

但我有这个错误:

Error in subset(test.data, x < 50) : object 'test.data' not found 
4. subset(test.data, x < 50) at hello.R#8 
3. testpackage1::readData() 
2. print(testpackage1::readData()) at hello.R#8 
1. hello() 

如果我输入require(testpackage1) bef矿石调用方法hello(),那么它的工作。

但我想加载testpackage2会自动加载它的依赖关系。我可以在hello()函数中添加require(testpackage1),但对于@import语句似乎是多余的。

此外,readData() IS正确导入,为什么不是数据?我是否应该以某种方式导出数据?

回答

0

不知道这是一个错误或功能,但我做到了通过改变readData()方法testpackage1工作方式如下:

#' Reads data and transforms it 
#' 
#' @return a data.frame 
#' @export 
#' 
#' @examples my.df <- readData 
readData <- function() { 
    return(subset(testpackage1::test.data, x < 50)) 
} 

注意testpackage1::test.data

相关问题