如何使用roxygen记录数据集?

Kar*_* W. 51 r documentation-generation roxygen r-package

是否可以在roxygen过程中将.R文件包含在我的包的数据目录中?

我在数据目录中放了几个.R文件.当它们使用data()获取时,它们会读入原始数据文件并执行一些转换.

Sha*_*ane 44

Roxygen可以在R文件中的任何地方使用(换句话说,它不必跟随函数).它还可以用于记录R文档中的任何docType.

所以你可以在一个单独的块中记录你的数据(类似这样):

#' This is data to be included in my package
#'
#' @name data-name
#' @docType data
#' @author My Name \email{blahblah@@roxygen.org}
#' @references \url{data_blah.com}
#' @keywords data
NULL
Run Code Online (Sandbox Code Playgroud)

  • 除了你最好使用`NULL`而不是`roxygen()`,这样你就不会对`roxygen'产生运行时依赖性. (10认同)
  • @hadley:将这样的例子添加到roxygen插图中可能会很好,并且提出关于roxygen依赖性的观点?我发现在如何构造文件方面有点混乱. (3认同)
  • @Shane:我已经向roxygen开发者抱怨了,它应该在下一个版本中改变 (3认同)
  • 感谢Shane和Hadley的出色帮助.我现在看到更清楚如何使用roxygen; 现在R CMD检查不再抱怨了.剩下的一个问题是:我是否需要将数据文档放在R子目录中?教导roxygenize查看数据目录会很好... (2认同)
  • @Karsten:我倾向于认为数据子目录中唯一应该包含的是数据.Roxygen提供有文化的编程作为R代码,所以我喜欢在我的R文件中拥有它.但除此之外,您可以尝试这样:roxygenize使用环境变量"R.DIR".将其设置为"数据",它应该处理数据目录中的R文件.@hadley:你可以制作一个简单的补丁以允许R.DIR向量? (2认同)

had*_*ley 33

从roxygen2> 4.0.0开始,您可以通过记录定义为字符串的对象的名称来记录在别处定义的数据对象:

#' This is data to be included in my package
#'
#' @author My Name \email{blahblah@@roxygen.org}
#' @references \url{data_blah.com}
"data-name"
Run Code Online (Sandbox Code Playgroud)


Jer*_*lim 22

我发现研究ggplot2包中的例子很有用.

请参阅github上的ggplot2.r

一些注意事项:

  • 数据集的所有Roxygen代码都可以包含在包.rR目录中的单个文件中.

参见示例,diamonds数据集:

#' 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:
#'
#' \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 colour. 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)
#' }
#'
#' @docType data
#' @keywords datasets
#' @name diamonds
#' @usage data(diamonds)
#' @format A data frame with 53940 rows and 10 variables
NULL
Run Code Online (Sandbox Code Playgroud)

这会生成一个如下所示的帮助文件:

roxygen文档示例