'data' is not an exported object from 'namespace:my_package'

只谈情不闲聊 提交于 2020-03-18 11:27:12

问题


I'm writing a function that uses an external data as follow:

First, it checks if the data is on the data/ folder, if it is not, it creates the data/ folder and then downloads the file from github;

If the data is already on the data/ folder, it reads it, and perform the calculations.

The question is, when I run:

devtools::check()

it returns:

Error: 'data' is not an exported object from 'namespace:my_package'

Should I manually put something on NAMESPACE?

An example:

my_function <- function(x){
if(file.exists("data/data.csv")){
    my_function_calculation(x = x)
  } else {
    print("Downloading source data...")
    require(RCurl)
    url_base <-
 getURL("https://raw.githubusercontent.com/my_repository/data.csv")
    dir.create(paste0(getwd(),"/data"))
    write.table(url_base,"data/data.csv", sep = ",", quote = FALSE)
    my_function_calculation(x = x)
  }
}

my_function_calculation <- function(x = x){
    data <- NULL
    data <- suppressMessages(fread("data/data.csv"))
    #Here, I use data...
    return(data)
}

回答1:


It could not be the same in every case, but I've solved the problem by removing the data.R file on R/ folder.

data.R is a file describing all data presented in the package. I had it since the previous version of my code, that had the data built in, not remote (to be downloaded). Removing the file solved my problem.

Example of data.R:

#' Name_of_the_data
#'
#' Description_of_the_Data
#'
#' @format A data frame with 10000 rows and 2 variables:
#' \describe{
#'   \item{Col1}{description of Col1}
#'   \item{Col2}{description of Col2}
#' }
"data_name"



回答2:


No need to remove data.R in /R folder, you just need to decorate the documentation around the NULL keyword as follow:

#' Name_of_the_data
#'
#' Description_of_the_Data
#'
#' @format A data frame with 10000 rows and 2 variables:
#' \describe{
#'   \item{Col1}{description of Col1}
#'   \item{Col2}{description of Col2}
#' }
NULL



回答3:


When I was stumped by the error

Error: 'data' is not an exported object from 'namespace:my_package'

MrFlick's comment above saved me. I had simply changed the name of an .rda file in my data folder. I was unable to get devtools::document() to recreate the NAMESPACE file. The solution was to re-save the data into the .rda file. (Of course I should have remembered that when one loads from an .rda file the name of the R object(s) has nothing to do with the name of the .rda file so renaming the .rda file doesn't do much.)



来源:https://stackoverflow.com/questions/46837428/data-is-not-an-exported-object-from-namespacemy-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!