Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

accessing sysdata.rda within package functions

Tags:

r

r-package

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
}
like image 649
Mark Danese Avatar asked Oct 06 '15 08:10

Mark Danese


2 Answers

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.

like image 102
the_skua Avatar answered Nov 02 '22 05:11

the_skua


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.

like image 1
Stef van Buuren Avatar answered Nov 02 '22 05:11

Stef van Buuren