Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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

If the data is already in 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)
}
like image 463
Igor Avatar asked Oct 19 '17 19:10

Igor


4 Answers

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"
like image 182
Igor Avatar answered Oct 23 '22 18:10

Igor


Generally, this happens when you have a mismatch between the names of one of the rda files in data folder and what is described in R/data.R.

In this case, the data reference in the error message is for data.csv, not the data folder. You need to have rda files in the data folder of a R package. If you want to download csv, you need to put them in inst/extdata.

This being said, you might want to consider using tempdir() to save those files in the temp folder of your session instead.

like image 44
brunj7 Avatar answered Oct 23 '22 17:10

brunj7


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
like image 2
Mohammed Ali Avatar answered Oct 23 '22 17:10

Mohammed Ali


There's 3 things to check:

  1. The documentation is appropriately named:
#' 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
  1. That the RData file is appropriately named for export in the data/ folder.

  2. That the RData file is loaded with the name data.

If documentation (1) is A, the Rdata file is A.RData (2), but the object (when loaded with load() ) is named B- you're going to get this error exactly.

like image 2
Carlos R. Mercado Avatar answered Oct 23 '22 17:10

Carlos R. Mercado