R包:写入内部数据,但不能一次全部写入 [英] R package: writing internal data, but not all at once

查看:63
本文介绍了R包:写入内部数据,但不能一次全部写入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 usethis / devtools 开发R包.该软件包中有一些我想保留在内部的对象,只是为了防止混乱.我使用的结构是根据对象的来源将对象制作在不同的文件中,这些文件全部位于我的 data-raw 文件夹中.例如,文件 make_laus_codes.R 准备来自劳工统计局的两个查找代码数据帧(一个内部称为 laus_codes )和文件 make_decennial_tables.R 准备从十年一次的人口普查中查找代码(包括内部的 decennial_nums ).

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).

如果我拨打 usethis :: use_data(data_name,internal = TRUE)这样的调用,如果 sysdata.rda 文件已经创建并且我没有选择覆盖它;如果我选择覆盖,它将覆盖整个内容,而不是我所期望的内容,即将第二个对象附加到 sysdata.rda .

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.

在sysdata.rda中存储多个对象的公认答案:R-package开发表示调用 usethis:: use_data(laus_codes,decennial_nums,internal = TRUE),但是这里的注释提出了一个问题,即如果不是同时创建这些对象该怎么办,这就是我要提取的地方.

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)

这有效,但是感觉很尴尬,就像我错过了执行此操作的预期方法一样.有没有一种方法可以做到更好,更正确和/或由 usethis 使用?以这种方式采购其他文件时容易受到错误和健忘的影响.

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.

推荐答案

虽然此解决方案不使用 usethis ,但我相信它可以简洁地解决您的问题:

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")

这篇关于R包:写入内部数据,但不能一次全部写入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆