25

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
}
Jack Wasey
  • 3,360
  • 24
  • 43
Mark Danese
  • 2,331
  • 1
  • 20
  • 25

2 Answers2

15

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.

patL
  • 2,259
  • 1
  • 17
  • 38
the_skua
  • 1,230
  • 17
  • 30
3

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.

Stef van Buuren
  • 348
  • 1
  • 10