说实话,我花了很长时间仔细阅读手册,问自己同样的问题。 去做吧,这是个好主意,很有用,而且有一些工具可以帮助你。 Writing help extension 手册描述了您可以保存数据的格式,以及如何遵循 R 标准。
我建议在包中提供数据是使用:
devtools::use_data(...,internal=FALSE,overwrite=TRUE)
其中... 是您要保存的数据集的不带引号的名称。
https://www.rdocumentation.org/packages/devtools/versions/1.13.3/topics/use_data
您只需在包的inst 子目录中创建一个文件来创建数据集。我自己的例子是https://github.com/cran/stacomiR/blob/master/inst/config/generate_data.R
例如我用它来创建 r_mig 数据集
#################################
# generates dataset for report_mig
# from the vertical slot fishway located at the estuary of the Vilaine (Brittany)
# Taxa Liza Ramada (Thinlip grey mullet) in 2015
##################################
#{ here some stuff necessary to generate this dataset from my package
# and database}
setwd("C:/workspace/stacomir/pkg/stacomir")
devtools::use_data(r_mig,internal=FALSE,overwrite=TRUE)
这将以适当的格式保存您的数据集。使用internal = FALSE 允许所有使用data() 的用户访问。我建议您阅读data() 帮助文件。您可以使用data() 访问您的文件,包括当您不在包中时,只要它们位于数据子目录中。
如果 lib.loc 和 package 都为 NULL(默认),则数据集为
在所有当前加载的包中搜索,然后在“数据”中搜索
当前工作目录的目录(如果有的话)。
如果您使用的是 Roxygen,请创建一个名为 data.R 的 R 文件,其中存储所有数据集的描述。下面是 stacomIR 包中一个数据集的 Roxygen 命名示例。
#' Video counting of thin lipped mullet (Liza ramada) in 2015 in the Vilaine (France)
#'
#' This dataset corresponds to the data collected at the vertical slot fishway
#' in 2015, video recording of the thin lipped mullet Liza ramada migration
#'
#' @format An object of class report_mig with 8 slots:
#' \describe{
#' \item{dc}{the \code{ref_dc} object with 4 slots filled with data corresponding to the iav postgres schema}
#' \item{taxa}{the \code{ref_taxa} the taxa selected}
#' \item{stage}{the \code{ref_stage} the stage selected}
#' \item{timestep}{the \code{ref_timestep_daily} calculated for all 2015}
#' \item{data}{ A dataframe with 10304 rows and 11 variables
#' \describe{
#' \item{ope_identifiant}{operation id}
#' \item{lot_identifiant}{sample id}
#' \item{lot_identifiant}{sample id}
#' \item{ope_dic_identifiant}{dc id}
#' \item{lot_tax_code}{species id}
#' \item{lot_std_code}{stage id}
#' \item{value}{the value}
#' \item{type_de_quantite}{either effectif (number) or poids (weights)}
#' \item{lot_dev_code}{destination of the fishes}
#' \item{lot_methode_obtention}{method of data collection, measured, calculated...}
#' }
#' }
#' \item{coef_conversion}{A data frame with 0 observations : no quantity are reported for video recording of mullets, only numbers}
#' \item{time.sequence}{A time sequence generated for the report, used internally}
#' }
#' @keywords data
"r_mig"
完整的文件在那里:
https://github.com/cran/stacomiR/blob/master/R/data.R
另一个例子:阅读:http://r-pkgs.had.co.nz/data.html#documenting-data
然后你可以通过调用data("r_mig")在下面的测试中使用这些数据
test_that("Summary method works",
{
... #some other code
data("r_mig")
r_mig<-calcule(r_mig,silent=TRUE)
summary(r_mig,silent=TRUE)
rm(list=ls(envir=envir_stacomi),envir=envir_stacomi)
})
最重要的是,您可以使用手册中的内容来描述如何使用包中的函数。