2017-02-22 34 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

相關問題