How to use/modify a dataset internal to an R package

I'm developing an R package that relies on a dataset that is essentially a lookup table. I don't expect that the user to need to save the dataset so I don't want it listed in the Environment pane. However, I do want to allow the user to modify the dataset in controlled ways. The dataset itself is relatively small (25 rows and 4 columns), and essentially serves as a lookup table.

I generated the data and saved it in the data\ directory in my package. I wrote a very small documentation file for the dataset and made sure to not export() the dataset.

Within the "getter" functions, which extract information from the dataset, it looks like I can call the name of the dataset just fine. Within the "setter" functions, where I modify the dataset, I'm using <<- to do so.

Should this system work? I don't know if I can provide a reprex, I keep getting an error message, cannot change value of locked binding for the dataframe when I run devtools::check().

I'm just thinking out loud here but what about creating a dot-prefixed copy of the data set when the package namespace is loaded with .onLoad(). This way the data set won't show up in the Global Environment pane, and you can access and modify this without needing to touch the original data set in the package internals.

# zzz.R
.onLoad <- function(...) {
  assign(".my_hidden_data", package:::dataset, envir = globalenv())
}

Not sure you even need the triple-colon namespacing, since .onLoad() lives in your package and should have access to internal components, but I haven't tested this.

3 Likes

This is a great idea! I haven't used a dot-prefixed dataset before or done much on loading, so I'll have to play around with it. Thanks for the suggestion!

Dot-objects are hidden by default (similar to dot-files), but they are normal objects in every other sense. The user will still be able to access the hidden data set, but they are less likely to do this if they don't know about it/can't see it in the Environment pane.

2 Likes

Thanks again for this recommendation! I tried it out and it seems to work exactly how I want!

I was familiar with dot prefixed functions - but never considered dot prefixed datasets. An obvious extension - by hindsight. Thanks for sharing!

2 Likes

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.