17.6 为包撰写文档
把函数的帮助页面放在不同目录的最大的问题是很容易使内容不同步。这方面的一个典型例子是当你要添加、删除或重命名函数参数时。 R不能自动更改相应的帮助文件来匹配函数的更改,你必须同时修改两个文件才能确保它们之间能一直保持更新同步。
roxygen2
包通过让你在R的代码注释中直接使用帮助文本解决了这个问题。它还能通过使用一个简单的标记语言,大大减少了对LaTeX知识的依赖。roxygen2
继承自Doxygen
,它是一个能生成类似C++、C、Java、Fortran、Python和其他语言文档的工具。它的语法非常值得学习,使你能为其他许多语言生成文档。
roxygen2
标记的每一行以#'
开头。有些部分,如标题和描述,是通过它们在注释段的开始位置来标记。其他部分则以关键字标注。例如,描述返回值的部分使用@return
开头来标注。一个完整的帮助文本段如下所示:
#' Help page title
#'
#' A couple of lines of description about the function(s).
#' If you want to include code, use \code{my_code()}.
#' @param x Description of the first argument.
#' @param y Description of the second argument.
#' @return Description of the return value from a function.
#' If it returns a list, use
#' \itemize{
#' \item{item1}{A description of item1.}
#' \item{item2}{A description of item2.}
#' }
#' @note Describe how the algorithm works, or if the function has
#' any quirks here.
#' @author Your name here!
#' @references Journal papers, algorithms, or other inspiration here.
#' You can include web links like this
#' \url{http://www.thewebsiteyouarelinkingto.com}
#' @seealso Link to functions in the same package with
#' \code{\link{a_function_or_dataset}}
#' and functions in other packages with
#' \code{\link[another_package]{a_function_or_dataset}}
#' @examples
#'
#R code run by the example function
#' \dontrun{
#'
#R code that isn't run by example or when the package is built
#' }
#' @keywords misc
#' @export f <- function(x, y)
{
#Function content goes here, as usual
}
在上例中,有几件事情需要特别注意。
参数需要使用@parm
关键字标注。(术语“parm”在整个Doxygen系统中都是标准的,所以如果想在R中把它改为“arg”会带来更多的混乱,还不如删除它。)@parm
参数后面跟着一个空格、参数名、另一个空格,以及参数的描述。
范例中的任何东西都必须是合法的R程序,因为当你构建软件包时它会自动运行。如果你想添加注释,请使用一个额外的哈希符#
(在现有的用于roxygen2
的#'
号顶部)来创建。如果你想补充一些失败情景下(例如演示创建文件时发生错误)的范例,可以把它们包装在一个\dontrun{}
块中。
帮助文件可包含关键字,但不是所有关键字都可以。请安装R.oo
包来查看所有可能值的列表,并运行此代码片段:
library(R.oo)
Rdoc$getKeywords()
(或者也可以打开R.home ("doc")
返回目录中的KEYWORD的文件。 )
添加@export
关键字将列出NAMESPACE文件中的函数,而这意味着用户应该能够从此包中调用该函数,而不仅仅是一个内部的辅助函数。
整个包的描述文档位于一个名为packagename-package.R
的文件中。它类似于函数的文档,但可能更容易撰写,因为它的内容更加少:
#' Help page title. Probably the package name and tagline.
#'
#' A description of what the package does, why you might want to use it,
#' which functions to look at first, and anything else that the user
#' really, absolutely, must look at because you've created it and it is
#' astonishing.
#'
#' @author You again!
#' @docType package
#' @name packagename
#' @aliases packagename packagename-package
#' @keywords package NULL
函数文档中有两处位非常重要,一个是@docType package
行,它告诉roxygen2
这是整个包的文档;另一个是最后的NULL
值,它的出现是由于技术的原因——如果你忽略它就会产生错误。
为数据集撰写文档几乎与为整个包撰写文档一样。对此文档的存放位置并没有特定的标准,你可以把它追加到包文档后面,又或者把它放到一个单独创建的packagename-data.R文件中:
#' Help page title
#'
#' Explain the contents of each column here in the description.
#' \itemize{
#' \item{column1}{Description of column1.}
#' \item{column2}{Description of column2.}
#' }
#'
#' @references Where you found the data.
#' @docType data
#' @keywords datasets
#' @name datasetname
#' @usage data(datasetname)
#' @format A data frame with m rows of n variables NULL
和包一样,对于数据集来说有两处很重要:告诉roxygen2
这是函数文档的@docType data
行,以及最后的NULL
值。
在你为每个函数、数据集和整个包撰写了文档之后,请调用roxygenize
函数生成帮助文件以及更新NAMESPACE和DESCRIPTION文件(对于某些更喜欢英式拼写法的人来说,可以使用roxygenize
来替代roxygenise
):
roxygenize("path/to/root/of/package")