rpackage-development

How do I load data from another package from within my package


One of the functions in a package that I am developing uses a data set from the acs:: package (the fips.state object). I can load this data into my working environment via

data(fips.state, package = "acs"),

but I do not know the proper way to load this data for my function. I have tried

 @importFrom acs fips.state,

but data sets are not exported. I do not want to copy the data and save it to my package because this seems like a poor development practice.

I have looked in http://r-pkgs.had.co.nz/namespace.html, http://kbroman.org/pkg_primer/pages/docs.html, and https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Data-in-packages, but they do not include any information on sharing data sets from one package to another.

Basically, how do I make a data set, that is required by functions in another package available to the functions in my package?


Solution

  • If you don't have control over the acs package, then acs::fips.state seems to be your best bet, as suggested by @paleolimbot.

    If you are going to make frequent calls to fips.state, then I'd suggest making a local copy via fips.state <- acs::fips.state, as there is a small cost in looking up objects from other packages that you might do well to avoid incurring multiple times.

    But if you are able to influence the acs package (even if you are not, I think this is a useful generalization), then mikefc suggests an alternative solution, which is to set the fips.state object up as internal to the package, and then to export it:

    usethis::use_data(fips.state, other.data, internal = FALSE)
    

    And then in NAMESPACE:

    export(fips.state)

    or if using roxygen2:

    #' Fips state
    #' @name fips.state
    #' @export
    "fips.state"
    

    Then in your own package, you can simply @importFrom acs fips.state.