Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R package: writing internal data, but not all at once

I'm working on an R package using usethis/devtools. The package has a few objects I'd like to keep internal, just to keep down the clutter. The structure I was using was to make objects in different files based on their source, all in my data-raw folder. For instance, the file make_laus_codes.R preps two data frames of lookup codes from the Bureau of Labor Statistics (one internal, called laus_codes), and the file make_decennial_tables.R preps lookup codes from the Decennial Census (including an internal, decennial_nums).

If I make a call like usethis::use_data(data_name, internal = TRUE), I get an error if the sysdata.rda file has already been created and I haven't chosen to overwrite it; if I choose to overwrite, it overwrites the whole thing, rather than what I'd expected, which is appending the second object to sysdata.rda.

The accepted answer to Store multiple objects in sysdata.rda: R-package development says to call usethis::use_data(laus_codes, decennial_nums, internal = TRUE), but a comment there poses the question of what if these objects aren't being created at the same time, and that's where I'd like to pick up.

A simplified version of my structure is as follows:

data-raw/make_laus_codes.R:

laus_codes <- data.frame(
  area = c("Connecticut", "Fairfield County", "Hartford County"),
  code = c("ST0900000000000", "CN0900100000000", "CN0900300000000")
)

data-raw/make_decennial_tables.R:

decennial_nums <- c("H002", "H003", "H004", "H005", "H006")

data-raw/make_internal_data.R:

source("./make_laus_codes.R")
source("./make_decennial_tables.R")

usethis::use_data(laus_codes, decennial_nums, internal = TRUE)

This works, but it feels awkward and like I'm missing the intended way to do this. Is there a way to do this that is better, more proper, and/or intended by usethis? It feels susceptible to bugs and forgetfulness to be sourcing other files this way.

like image 795
camille Avatar asked Jul 17 '18 14:07

camille


1 Answers

While this solution doesn't use usethis, I believe it solves your problem concisely:

# Let's say you've saved this sysdata in the past
laus_codes <- data.frame(
    area = c("Connecticut", "Fairfield County", "Hartford County"),
    code = c("ST0900000000000", "CN0900100000000", "CN0900300000000")
)
usethis::use_data(laus_codes, internal = TRUE)

# Updating sysdata with objects you've created just now
decennial_nums <- c("H002", "H003", "H004", "H005", "H006")
sysdata_filenames <- load("R/sysdata.rda")
save(list = c(sysdata_filenames, "decennial_nums"), file = "R/sysdata.rda")
like image 188
David Rubinger Avatar answered Oct 16 '22 07:10

David Rubinger