【问题标题】:Include data examples in developing R packages在开发 R 包中包含数据示例
【发布时间】:2012-09-05 15:38:22
【问题描述】:

我很想学习如何将数据示例合并为写在函数之上的 cmets,例如:

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


    【解决方案1】:

    Hadley Wickham 在他的书中有一个章节 "R Packages" how to incorporate data into an R Package

    Dirk 指向data in packages 上的官方文档。

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

    Here is the data directory in the ggplot2 package。在此示例中,每个数据文件都存储在单独的 rda 文件中(例如,使用 save(foo, file='foo.rda') 生成。

    请参阅the file 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"
    

    【讨论】:

    【解决方案2】:

    请查看包含数据的 CRAN 包并复制他们的方法。几周前,我刚刚将数据添加到一个仅限工作的包中,它可以正常工作...

    对于它的价值,手册中有一节 1.1.5 Data in packages 对此进行了解释。

    【讨论】:

      【解决方案3】:
      x <- sample(1000)
      devtools::use_data(x, mtcars)
      

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

      【讨论】:

      • 感谢您提供代码 sn-p,它可能会提供一些有限的即时帮助。正确的解释将通过描述为什么这是解决问题的好方法来极大地改进其long-term value,并使其对未来有其他类似问题的读者更有用。请编辑您的答案以添加一些解释,包括您所做的假设。
      • 应该是usethis::use_data(x, mtcars),因为显示的是贬义
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-10
      • 1970-01-01
      • 2020-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多