为 R 项目创建文档
Create documentation for an R project
我想为我的 R 代码创建文档。代码是 R 项目的一部分,而不是包的一部分。有没有一种方法可以使用内置帮助查看器显示不在包中的代码文档,而无需创建完整包?
如果您愿意将文档直接保存在代码文件中,则可以在函数上方使用 roxygen 样式注释。但是,您将无法使用典型的 ?your_function
语法轻松查看文档。可能有一些方法可以通过一些 hack 来生成文档并将它们放在帮助搜索路径中的某个位置,但这似乎比必要的工作更多。
如果您愿意在函数中包含 roxygen2 样式文档,如果您愿意加载 docstring
包,则可以获得漂亮的 ?your_function
语法来查看文档。这是我为您的用例编写的一个包——您有想要记录的代码,但没有花时间或不想将其放入包中。我建议要么阅读 the github page for docstring or view the vignette provided on the cran page for docstring.
上的自述文件
这是一个使用文档字符串的示例会话:
library(docstring)
square <- function(x){
#' Square a number
#'
#' Calculates the square of the input
#'
#' @param x the input to be squared
return(x^2)
}
# This will display the documentation for square
# just like any other help file would be displayed
?square
我想为我的 R 代码创建文档。代码是 R 项目的一部分,而不是包的一部分。有没有一种方法可以使用内置帮助查看器显示不在包中的代码文档,而无需创建完整包?
如果您愿意将文档直接保存在代码文件中,则可以在函数上方使用 roxygen 样式注释。但是,您将无法使用典型的 ?your_function
语法轻松查看文档。可能有一些方法可以通过一些 hack 来生成文档并将它们放在帮助搜索路径中的某个位置,但这似乎比必要的工作更多。
如果您愿意在函数中包含 roxygen2 样式文档,如果您愿意加载 docstring
包,则可以获得漂亮的 ?your_function
语法来查看文档。这是我为您的用例编写的一个包——您有想要记录的代码,但没有花时间或不想将其放入包中。我建议要么阅读 the github page for docstring or view the vignette provided on the cran page for docstring.
这是一个使用文档字符串的示例会话:
library(docstring)
square <- function(x){
#' Square a number
#'
#' Calculates the square of the input
#'
#' @param x the input to be squared
return(x^2)
}
# This will display the documentation for square
# just like any other help file would be displayed
?square