I thought that putting an internal dataset for a package into R/sysdata.rda
would make the data accessible to my functions. But I can't seem to figure out how to actually access this dataframe. None of the documentation actually says how to access the data, but my guess was that I could simply refer to the dataframe by name. However, this does not seem to work.
I used devtools::use_data()
with internal = TRUE
and sysdata.rda was created. Lazy-loading is set to TRUE.
To test it, I manually loaded it just to make sure it was the right file. The file is called nhanes_files
. Within my function, I simply refer to the nhanes_files
object and extract the necessary data. When I tested my function in my package project, it seemed to work. When I build and load the package, upload to GitHub, and then install the package into a new project, I get an error: Error in find_data() : object 'nhanes_files' not found
Do I need to do something else to make this internal data accessible to my functions?
Below is the most basic function, which is not working:
#' Print NHANES file listing
#'
#' Provides access to the internal data listing all NHANES files
#'
#' @return A data frame with the list of files that can be accessed through the NHANES website. Should not generally be used. Present for debugging purposes and transparency.
#' @export
find_data <- function(){
nhanes_files
}
If your package name is somepackage
and the object saved was nhanes_files
with devtools::use_data(nhanes_files, internal = TRUE)
then you can access this in your functions by calling somepackage:::nhanes_files
.
Pay attention, there're 3 :
here.
I use myobject <- get0("myobject", envir = asNamespace("mypackage"))
.
This formulation passes R CMD CHECK
. It is possible to change the name of the value, and it works to access objects in other loaded packages.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With