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

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

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"

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

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.

There's 3 things to check:
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
That the RData file is appropriately named for export in the data/ folder.
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.

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"

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.

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

I spent a few hours trying to fix this. Finally got it to work.
Notes:
Data files have to be of type "rda". "rds" won't work.
File names had to be lower case.
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.

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.

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

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

I had the same issue with one of my packages, and I needed to add
LazyData: true
to my DESCRIPTION file.

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

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.

Related

R calling a dataset in the package itself

I have created a package containing a dataset called mydata.
I want to use my dataset in my functions but I dont know how to call it.
when i use data("mydata") to call my dataset and avoid warning messages I have another message during the building process
See section ‘Good practice’ in ‘?data’.
#imporFrom mypackage mydata doesn't work either. What's the best way to call a dataset in the package itself?
Use package_name::mydata, example in car package car::Angell.
Dataset is in a data folder with for example mydata.rda inside.
To add help, create a data.R file in R folder.
Personnaly I put inside :
# 1. mydata ----
#' Here is a long title
#'
#' Description of the dataset : length, columns
#'
#' #format one object of class XXX
#'
#' #source where it comes from
#' #name mydata
NULL

Can't access dataset documentation in R

I have created a package using devtools and roxygen2, which works perfectly fine. Now I want to include two data frames termed mydata1 and mydata2 into the package.
I have saved the data frames in mypackage/data as mydata1.rda and mydata2.rda using devtools::use_data(mydata1, mydata2)
Then I created a mydata1.R and mydata2.R files in mypackage, which contains the description as follows:
#' My temperature data
#'
#' A dataset containing the temperature
#'
#' #format A data frame with 153 rows and 1 variable:
#' \describe{
#' \item{temperature}{relative air temperature in degree Celsius}
#' }
"mydata1"
However, after I install and restart in the package build mode, I cannot access the description via help(mydata1). I can access the data via mypackage::mydata1 though.
What am I doing wrong? It might be a stupid mistake, however, I could not find a solution so far. Thank you very much in advance.
Ok I found the solution, I just needed to save the description files in mypackage/R and not in mypackage.

R package data not available when importing in another package

I have one package "testing" with a data object "test_data" saved in data folder under file name "test_data.RData".
testing contains one function hello() that uses this data object
#' hello
#'
#' #return Prints hello "your_name"
#' #export
#'
#' #examples
#' hello()
hello <- function(your_name = "") {
print(paste("test_data has", nrow(test_data), "rows"))
print(sprintf("Hello %s!", your_name))
}
the following code works fine:
require(testing)
testing::hello()
[1] "test_data has 32 rows"
[1] "Hello !"
but this fails:
testing::hello()
Error in nrow(test_data) : object 'test_data' not found
Actually I do not use it directly but in another package testingtop that imports this function:
#' Title
#'
#' #export
#' #importFrom testing hello
hello2 <- function(){
hello()
}
I have testing in the Imports section of DESCRIPTION and this fails.
require(testingtop)
testingtop::hello2()
Error in nrow(test_data) : object 'test_data' not found
If I put it in Depends it works if I load the package with library()
otherwise it still fails:
> library(testingtop)
Loading required package: testing
> testingtop::hello2()
[1] "test_data has 32 rows"
[1] "Hello !"
Restarting R session...
> testingtop::hello2()
Error in nrow(test_data) : object 'test_data' not found
if it was a function instead of a data object Imports would be fine, why is it different with a data object and I need to load the imported package? Did I miss something? And is it related to LazyData and LazyLoad ?
Probably a duplicate of this question
SO I think I've found the solution from the doc of the data function ?data
Use of data within a function without an envir argument has the almost always undesirable side-effect of putting an object in the user's workspace (and indeed, of replacing any object of that name already there). It would almost always be better to put the object in the current evaluation environment by data(..., envir = environment()). However, two alternatives are usually preferable, both described in the ‘Writing R Extensions’ manual.
For sets of data, set up a package to use lazy-loading of data.
For objects which are system data, for example lookup tables used in calculations within the function, use a file ‘R/sysdata.rda’ in the package sources or create the objects by R code at package installation time.
A sometimes important distinction is that the second approach places objects in the namespace but the first does not. So if it is important that the function sees mytable as an object from the package, it is system data and the second approach should be used.
Putting the data in the internal data file made my function hello2() see it
> testingtop::hello2()
[1] "test_data has 32 rows"
[1] "Hello !"
To supplement Benoit's answer. I had essentially this problem, but when using my package data as a default for a function parameter. In that case there's a third solution in the ?data help file: "In the unusual case that a package uses a lazy-loaded dataset as a default argument to a function, that needs to be specified by ::, e.g., survival::survexp.us."
This third approach solved it for me. (But I found it thanks to Benoit's link.)

Unable to load external R object with load()

I saved R objects in .Rdata files with save(obj, filename) but now I'm unable to load these objects in my global environment with load(filename).
Files are created in my hard disk with a size around 4.2ko so I guess they contain the data but Rstudio can't load them and load() returns no error.
What can I do to load these data, or at least verify the data are present ?
EDIT: When redirecting load() I can see the data is only a character string and I'm very disappointed of not being able to restore these data. It is true that I should have tested the file restoration earlier, but is there still a hope ? a file of 4.2ko must contain data.
> MYR <- load("/home/R/data/MYR.Rdata")
> MYR
[1] "data"
> class(MYR)
[1] "character"
This is not how load works (see).
It loads the data in the environment and return only a list of names of objects created.
So in your case an object named data has been loaded in the environment. You can confirm with ls() it is there. And of course you can inspect the object data.

Include example using data in documentation of that data (when developing R package)

In documentation of data within an R package (using Roxygen via Rstudio), is it possible to include an example that uses that data?
E.g. TestPackage.R in the R directory
#' My New Colour
#'
#' Enables use of my new colour \emph{aNewColour} in plots.
#' #name aNewColour
#' #docType data
#' #format A colour defined by rgb(red=232,green=81,blue=0,maxColorValue=255)
#' #usage data(allNewColours)
#' #examples
#' curve(dnorm,from=-4,to=4,col=aNewColour,lwd=2)
NULL
In the same R directory, I have a file: allNewColours.rda, which consists only of the variable aNewColour with value #E85100.
When I press Check on the Build menu of R studio (equivalent to the command R CMD Check I believe), I get the following error message:
** Examples
curve(dnorm,from=-4,to=4,col=aNewColour,lwd=2) Error in plot.xy(xy, type, ...) : object 'aNewColour' not found Calls: curve -> plot ->
plot.default -> plot.xy Execution halted Error: Command failed (1)
Execution halted
Exited with status 1.
Is it possible to use the data "aNewColour" in the example of the documentation of "aNewColour" ?
Edit: Okay I found my problem - If I move the .rda file to a data folder (at the same level as the R folder, all works)... Had been struggling with this for ages only to determine the solution within seconds of posting this question ...
move the .rda file to a data folder - and enter LazyData: yes in the DESCRIPTION file

Resources