如何在构建 R 包时向导出的数据添加列表?

How to add a list to exported data when building up R packages?

我在 my_package/data 中将两个列表保存为“.RData”,并将它们的文档保存在 /R 中。但是安装后,我无法加载这两个列表,但它显示了帮助文档。 是否可以导出 R 包中的列表?或者只是能够矩阵或类似的东西。我对“一个 .Rdata 文件中的一个对象”感到困惑

Hadley 的在线书籍 R Packages (and particularly the chapter on data) 在描述如何包含数据方面做了更详尽的工作,但我将介绍如何使任意对象在包中有用。

前期:

  • 一个拥有 私有 数据(无需更改即可访问 same-package 函数,使用 ::: 在包外访问)和 public 数据(内部函数和任何加载包的人都可用的数据,可选 lazy-loaded)
  • public数据存放在./mypackage/data/目录下,使用*.rda 扩展名 (*.Rdata) 不起作用;一个常见的约定(为了 version-control、方便、可维护性)虽然不是严格的要求,但每个 .rda 文件一个对象,并命名为对象名称;下面,我将 mypubliclist 存储在 ./mypackage/data/mypubliclist.rda;
  • private数据应该全部保存在一个文件中,./mypackage/R/sysdata.rda.

这是一个示例包(我正在使用 devtools,但还有其他方法可以这样做):

devtools::create("~/Whosebug/14489611/mypackage")
# v Creating 'C:/Users/r2/Whosebug/14489611/mypackage/'
# v Setting active project to 'C:/Users/r2/Whosebug/14489611/mypackage'
# v Creating 'R/'
# v Writing 'DESCRIPTION'
# Package: mypackage
# Title: What the Package Does (One Line, Title Case)
# Version: 0.0.0.9000
# Authors@R (parsed):
#     * First Last <first.last@example.com> [aut, cre] (YOUR-ORCID-ID)
# Description: What the package does (one paragraph).
# License: `use_mit_license()`, `use_gpl3_license()` or friends to pick a
#     license
# Encoding: UTF-8
# LazyData: true
# Roxygen: list(markdown = TRUE)
# RoxygenNote: 7.1.1
# v Writing 'NAMESPACE'
# v Changing working directory to 'C:/Users/r2/Whosebug/14489611/mypackage/'

我写了一个小脚本 ./data-raw/mylist.R 来创建一些随机数据。此文件仅用于重现性,在包生产或加载期间不会引用它。

set.seed(42) # R-4.0.2
myprivatelist <- list(a = sample.int(10), b = sample.int(1e5))
mypubliclist <- list(a=1:10, b=11:20)
save(mypubliclist, file="~/Whosebug/14489611/mypackage/data/mypubliclist.rda")
save(myprivatelist, file="~/Whosebug/14489611/mypackage/R/sysdata.rda")

我还创建了一个 R 文件 ./mypackage/R/data.R(您可以任意命名此文件,但它必须位于 ./mypackage/R 下)以包含这些列表的简单文档:

#' My private list
#' @format list with random numbers
"myprivatelist"

#' My public list
#' @format list with not-so-random numbers
"mypubliclist"

现在生成文档:

devtools::document("C:/Users/r2/Whosebug/14489611/mypackage")
# Updating mypackage documentation
# Loading mypackage
# Writing myprivatelist.Rd
# Writing mypubliclist.Rd

我将构建包,重新启动 R,安装然后加载数据集。

devtools::build("~/Whosebug/14489611/mypackage")
# v  checking for file 'C:\Users\r2\Whosebug489611\mypackage/DESCRIPTION'
# -  preparing 'mypackage':
# v  checking DESCRIPTION meta-information
# -  checking for LF line-endings in source and make files and shell scripts
# -  checking for empty or unneeded directories
#      NB: this package now depends on R (>= 3.5.0)
#      WARNING: Added dependency on R >= 3.5.0 because serialized objects in  serialize/load version 3 cannot be read in older versions of R.  File(s) containing such objects: 'mypackage/R/sysdata.rda'  'mypackage/data/mypubliclist.rda'
# -  building 'mypackage_0.0.0.9000.tar.gz'
#    
# [1] "C:/Users/r2/Whosebug/14489611/mypackage_0.0.0.9000.tar.gz"

## restart R
install.packages("C:/Users/r2/Whosebug/14489611/mypackage_0.0.0.9000.tar.gz")
# Installing package into 'C:/Users/r2/R/win-library/4.0'
# (as 'lib' is unspecified)
# inferring 'repos = NULL' from 'pkgs'
# * installing *source* package 'mypackage' ...
# ** using staged installation
# ** R
# ** data
# *** moving datasets to lazyload DB
# ** byte-compile and prepare package for lazy loading
# ** help
# *** installing help indices
#   converting help for package 'mypackage'
#     finding HTML links ... done
#     myprivatelist                           html  
#     mypubliclist                            html  
# ** building package indices
# ** testing if installed package can be loaded from temporary location
# *** arch - i386
# *** arch - x64
# ** testing if installed package can be loaded from final location
# *** arch - i386
# *** arch - x64
# ** testing if installed package keeps a record of temporary installation path
# * DONE (mypackage)

让我们看看是什么起作用了:

str(mypubliclist)
# Error in str(mypubliclist) : object 'mypubliclist' not found
#     x
#  1. \-utils::str(mypubliclist)
library(mypackage)
str(mypubliclist)
# List of 2
#  $ a: int [1:10] 1 2 3 4 5 6 7 8 9 10
#  $ b: int [1:10] 11 12 13 14 15 16 17 18 19 20
str(myprivatelist)
# Error in str(myprivatelist) : object 'myprivatelist' not found
#     x
#  1. \-utils::str(myprivatelist)
str(mypackage:::myprivatelist)
# List of 2
#  $ a: int [1:10] 1 5 10 8 2 4 6 9 7 3
#  $ b: int [1:100000] 47128 16740 61605 73236 9091 62041 33700 59359 54789 54586 ...

及其文档,如果您感兴趣的话。 (我使用 print 因为否则它会在另一个窗格中弹出;这只是将它转储到控制台。)

print(help("mypubliclist"))
# mypubliclist             package:mypackage             R Documentation
# My public list
# Description:
#      My public list
# Usage:
#      mypubliclist
#      
# Format:
#      list with not-so-random numbers
print(help("myprivatelist"))
# myprivatelist            package:mypackage             R Documentation
# My private list
# Description:
#      My private list
# Usage:
#      myprivatelist
#      
# Format:
#      list with random numbers