7

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?

itsMeInMiami
  • 2,324
  • 1
  • 13
  • 34

2 Answers2

3

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.

Martin Smith
  • 3,687
  • 1
  • 24
  • 51
  • 1
    When I try to export a data set as you suggest, I am given the "Objects listed as exports, but not present in namespace" error. Also, the recommendation [here](https://r-pkgs.org/data.html) is to never export data. Can you comment on this? – Giovanni Colitti Jan 31 '21 at 01:33
2

You can always use package::object_name (e.g., dplyr::starwars) anywhere in your package code, without using an import statement.


is_starwars_character <- function(character) {
  character %in% dplyr::starwars$name
}
is_starwars_character("Luke Skywalker")
#> [1] TRUE
is_starwars_character("Indiana Jones")
#> [1] FALSE
paleolimbot
  • 406
  • 2
  • 3