Search code examples
rpackage

Access data of R package within its function when not loaded


So I need to use data in my R package functions. All is well if I do library(myPkg) and then use the function I need e.g.:

library(myPkg)
c_my_data(2)
[1] "A" "2"

But I can't use the function if I don't attach the package.

Restarting R session...

myPkg::c_my_data(2)
Error in myPkg::c_my_data(2) : object 'my_data' not found

I use roxygen2 to generate the NAMESPACE file and it says:

Datasets: all datasets are publicly available. They exist outside of the package namespace and must not be exported.

The NAMESPACE file:

# Generated by roxygen2: do not edit by hand

export(c_my_data)

The c_my_data.R file:

#' Data Adder
#' @param x a number
#' @export
c_my_data<- function(x) {
  c(my_data, x)
}

Creating my_data:

#called from datsets.R in project data-raw folder
my_data <- c("A")
usethis::use_data(my_data)

The data.R file:

#' My data
#' A vector
#' @format ## `my_data`
#' A vector
#' \describe{
#'   \item{A}{Letter}
#' }
#' @source Me
"my_data"

Is there any way I can add the data so that myPkg::c_my_data(2) would work?


Solution

  • Just calling my_data works only if the data has been previously loaded into the local environment once, by either:

    • library(MyPkg)
    • MyPkg::my_data
    • data("my_data", env = environment())

    When you did not use library(MyPkg) but also none of the other options in your script, my_data was never initially loaded.

    But when you used library(MyPkg), everything worked, which seems logical.

    In this answer to a similar question, the solution is to use data() once in the beginning to access package data inside the function.

    As kindly pointed out in the comments (@user2554330), the option MyPkg::my_data is working, not because it tells R where to find my_data - but because it has the side effect of loading the data into the local environment.

    The function myPkg::c_my_data() will be able to see all private variables in the package, and as soon as you call it, myPkg will be loaded (but not put on the search list).

    my_data can be addressed inside the function without specifying the package origin. The reason it is not found is that it was not loaded, but not because location was unclear.