在开发R包时包括数据示例

在开发R包时包括数据示例,r,package,roxygen2,R,Package,Roxygen2,我渴望学习如何将数据示例合并为函数上方的注释,例如: ##' @examples ##' ## Set working directory... ##' ## Load data into R session: ##' data <- system.file("extdata", "data.txt", package="...", sep="\t", header=TRUE, stringsAsFactors = FALSE) ##' ##' ## For reproducible r

我渴望学习如何将数据示例合并为函数上方的注释,例如:

##' @examples 
##' ## Set working directory...
##' ## Load data into R session:
##' data <- system.file("extdata", "data.txt", package="...", sep="\t", header=TRUE, stringsAsFactors = FALSE)
##'
##' ## For reproducible results:
##' set.seed(999)
##@示例
##“##设置工作目录。。。
##“##将数据加载到R会话:

##“数据请查看包含数据的CRAN包并复制其方法。几周前,我刚刚将数据添加到一个仅限工作的软件包中,它就可以正常工作了


不管它值多少钱,手册中有一节对此进行了解释。

哈德利·威克姆在他的书中有一章是关于这一点的

德克指的是关于这个问题的官方文件

或者,下面是一个学习ggplot2包的示例,了解如何使用rda文件和roxygen合并数据

。在此示例中,每个数据文件存储在单独的
rda
文件中(例如,使用
save(foo,file='foo.rda')
生成)

有关生成数据Rmd帮助文件的Roxygen命令,请参阅:例如

#' Prices of 50,000 round cut diamonds
#'
#' A dataset containing the prices and other attributes of almost 54,000
#'  diamonds. The variables are as follows:
#'
#' @format A data frame with 53940 rows and 10 variables:
#' \itemize{
#'   \item price: price in US dollars (\$326--\$18,823)
#'   \item carat: weight of the diamond (0.2--5.01)
#'   \item cut: quality of the cut (Fair, Good, Very Good, Premium, Ideal)
#'   \item color: diamond colour, from J (worst) to D (best)
#'   \item clarity: a measurement of how clear the diamond is
#'      (I1 (worst), SI1, SI2, VS1, VS2, VVS1, VVS2, IF (best))
#'   \item x: length in mm (0--10.74)
#'   \item y: width in mm (0--58.9)
#'   \item z: depth in mm (0--31.8)
#'   \item depth: total depth percentage = z / mean(x, y) = 2 * z / (x + y) (43--79)
#'   \item table: width of top of diamond relative to widest point (43--95)
#' }
"diamonds"

x感谢@Dirk的建议!请注意,现在还有一章介绍了向包中添加数据的各种方法:数据集描述已移至。感谢您提供的代码片段,它可能会提供一些有限的、即时的帮助。适当的解释将通过描述为什么这是一个很好的解决方案来大大改进its请编辑您的答案,添加一些解释,包括您所做的假设。应该是
use this::use_data(x,mtcars)
,如图所示
x <- sample(1000)
devtools::use_data(x, mtcars)