What is the best way of making a lot of objects available to users in a package?

I am creating a package and have a lot of variables that I want to make available in the environment to users. For example:

GRID_MAJOR_SIZE <- 0.5
GRID_MINOR_SIZE <- 0.25
LEGEND_POSITION <- "h"
LEGEND_X_OFFSET <- 0.3
LEGEND_Y_OFFSET <- -0.25

What is the best way of doing this? I can put it all in to the R folder in the package and that works, but I assume that I am meant to use the data-raw and data folders (like in here). But I don't want to save all the objects individually as .rdata files.

To me, that implies you want to gather them into a single object, that you can save and then serve up.
This might be a list, data.frame, or environment perhaps.

1 Like

So, would I move the files to data-raw then save everything in the workspace to data using something like save(file = “./data/constants.RData”).

Or is there a better way?

I note that the variables only become available after I run the full script with the save at the bottom. They don't just become available, rather than just being there (I think) with usethis::use_data().

What would you want the variables to be used for? It looks like the variables you've put are to be used in plotting but its rare that you'd need users to be able to access them individually and couldn't just have them as the default arguments to a function(s). The 'data' folder is typically for datasets and tables that are needed for examples.
In general it is very uncommon that you'd need to do it, but if you really wanted to, one method you could consider is just defining helper functions:

get_major_grid_size <- function() {0.5}
# or
get_grid_size <- function() {list(major = 0.5, minor = 0.25)}
#or
get_pkg_constants <- function() {list(grid.major.size = 0.5,
                                      grid.minor.size = 0.25,
                                      ...) #etc
2 Likes

The variables are mostly styling and configuration variables. Things like default colours for graphs, maps, and other things that would generally be used in a shiny application. These could definitely be put in as new arguments of functions and this is a better way of doing it.

The helper functions would probably work for the other types of variables that are not function arguments.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.