'data' is not an exported object from 'namespace:my_package'
Asked Answered
M

14

28

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)
}
Metrist answered 19/10, 2017 at 19:25 Comment(0)
M
19

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"
Metrist answered 24/10, 2017 at 12:46 Comment(3)
Any idea why this works? I had similar issue and resolution for data that was being used in a tutorial built with the learnr package. For functions this namespace error seems to get resolved by using :::: #2165842Adur
I had the same problem when I tried to rename a data set included with my package. In my case, the name of the df I used to create the file seems to be included in the .rda metadata, and NAMESPACE looks for that. I changed the name of the df to match the output file in the save command, and that took care of it. For example: save(my_df, file="data/my_df.rda"). R doesn't like it if "my_df" is saved with a different name.Hemo
This worked for me which is odd because my package has 2 datasets and 1 has the data.R file and I deleted the other one, but they both behave the same in the package.Paint
H
5

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.

Henrie answered 1/7, 2021 at 16:2 Comment(1)
Perfect answer to my situation. My problem was I'm using different object name when loading the dataset.Pedersen
Z
4

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.

Zoril answered 25/8, 2020 at 23:59 Comment(0)
C
3

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
Clavichord answered 27/4, 2019 at 5:39 Comment(0)
A
3

The problem probably is because how your object was named when you save it.

Suppose I load a file a called it "d", then I save it (as is suggested) with save in the data/ directory as "data":

save(d, file = "data/data.rda")

Then you will run the clean and install package and you will get the following error:

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

Looks like it does not matter how you declare your object in the roxygen documentation. I guess you must name your OBJECT with the same name you are going to save it and loaded it.

For example, load your dataset as "pib" object, then save as "pib.rda" and declare in roxygen "loadData.R" (for example) your "pib".

#' Datos del PIB
#'
#' @docType data
#'
#' @usage data(pib)
#'
#' @format An object of class ...
#'
#' @keywords datasets
#'
#' @references ----
#'
#' @source ----
#'
#' @examples
#' data(pib)
"pib"
Aramaic answered 1/8, 2022 at 16:12 Comment(0)
X
1

I spent a few hours trying to fix this. Finally got it to work.

Notes:

  1. Data files have to be of type "rda". "rds" won't work.
  2. File names had to be lower case.
  3. NULL in documentation name didn't work for me. Had to be a lower case string.

In general, it seems the same error message is caused by several things. Anything the checker doesn't like related to data files, it will issue the same error. Hard to debug under those circumstances.

Xeroderma answered 14/12, 2020 at 21:21 Comment(0)
C
1

I will add another trap. Working in RStudio I have assigned a string to MyString and saved in the data folder of my package project:

save(MyString, file="./data/MyString.RData")

My ./R/data.R file contains documentation for this:

#' A character string
#'
"MyString"

This works. But you must use one file per object and not do save(X, Y, Z, file="BitsAndPieces.RData") and then document BitsAndPieces. If you do then you will get the error of this question. Which I did, needless to say.

Chrominance answered 14/3, 2021 at 14:18 Comment(0)
C
1

I had the same issue with one of my packages, and I needed to add

LazyData: true

to my DESCRIPTION file.

Corundum answered 10/5, 2022 at 7:15 Comment(0)
H
1

I had this issue because I copied the .rda file into the R\data folder.

Issue was resolved by using usethis::use_data(DataObject) which automatically takes the raw-data (DataObject) file and adds it to the R\data folder within the R package directory.

Hubert answered 2/6, 2022 at 18:52 Comment(0)
O
0

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.)

Outoftheway answered 7/1, 2020 at 20:36 Comment(0)
M
0

I had the same error and I would be able to overcome the error as follows.

The data file located at: data/df.RData
The R documentation file located at: R/df.R

I have created the df.RData file by importing the df.txt file into R and using the save() function to create the .RData file. I used the following code block to create .RData file.

x=read.table("df.txt")
save(x,file="df.RData")

Then after running the RCMD check I get the same error as df is not an exported object from namespace "package name".

I have overcome the error by change the variable name of the df.RData file as

df=read.table("df.txt")
save(df,file="df.RData")
Megacycle answered 8/4, 2021 at 5:50 Comment(0)
B
0

Restarting the session solved the problem for me. Somehow the environment was empty and after restart all objects were back, hence solving the diff.

Boilermaker answered 22/4, 2022 at 7:16 Comment(0)
S
0

I had this problem, even renaming the variables and uninstalling the probematic packages didn't work.

I did:

I was trying to carry out the process in a session (tab) of R that was already in use previously, where the terra package had already been requested. This session is not saved, but was being automatically saved to an image in ~/.RData every time Rstudio was closed. So every time I opened Rstudio it retrieved that section (image) and reloaded the previous state causing the conflict between packages.

I solved it by creating a new blank rmarkdown and closing all previously opened sessions, as well as clearing all saved data in the Rstudio "Global environment".

Senecal answered 13/2, 2023 at 2:50 Comment(0)
P
-2

I encountered this "Error: 'weekly' is not an exported object from 'namespace:ISLR'' when I was trying the following:

library(ISLR)

w <- ISLR::weekly

The problem is somehow fixed by changing it to:

w = ISLR::weekly

The = sign made all the difference here.

Psycholinguistics answered 2/8, 2020 at 18:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.