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

问题描述 投票:0回答:3

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

##' @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)

我将“data.txt”文件放在目录:/pkg_Name/inst/extdata/中。然而,R CMD 检查表明此步骤有错误。如果我继续进行 R CMD 构建和 R CMD 安装,那么在加载包后,我无法将数据获取到 R 会话中...有人能告诉我出了什么问题吗?这是在函数帮助文档末尾包含数据示例的正确方法吗?

非常感谢!

r package roxygen2
3个回答
35
投票

Hadley Wickham 在他的书 “R Packages” 中有一章介绍了 如何将数据合并到 R 包中

Dirk 指出了关于 packages 中的数据的官方文档。

或者,这里有一个从 ggplot2 包中学习的示例,了解如何使用 rda 文件和 roxygen 合并数据的一种方法。

这里是ggplot2包中的

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

enter image description here

请参阅 文件 data.r,了解 Roxygen 命令以生成数据的 Rmd 帮助文件:例如,

#' 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"

12
投票

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

就其价值而言,手册中有一节1.1.5 包中的数据对此进行了解释。


2
投票
x <- sample(1000)
usethis::use_data(x, mtcars)

http://r-pkgs.had.co.nz/data.html

© www.soinside.com 2019 - 2024. All rights reserved.