我教了一门课,让学生使用我写的包。现在课程即将结束,我想为他们提供每个函数的代码,这些代码与函数的文档内联。我可以设置一个全局标志来完成此操作吗?某种代码破解?
问问题
99 次
2 回答
1
您可以使用 brew 包预处理您的 R 文件,例如
文件'foo-tmp.r'
##' a function that doesn't do much
##'
##' @title foo
##' @param x
##' @param y
##' @param z
##' @return error message
##' @author Baptiste
##' @examples
##' dontrun{
#<%= cat(paste0("##'", getSrcref(foo), "\n")) %> ##' }
foo <- function(x, y, z){
rnorm(10) == 1
# inline comment
.NotYetImplemented()
" other stuff"
return(FALSE)
}
然后处理文件生成foo.r
source("foo-tmp.r") # to know what the function is
brew("foo-tmp.r", "foo.r")
结果输出:
##' a function that doesn't do much
##'
##' @title foo
##' @param x
##' @param y
##' @param z
##' @return error message
##' @author Baptiste
##' @examples
##' dontrun{
###'function(x, y, z){
##' rnorm(10) == 1
##' # inline comment
##' .NotYetImplemented()
##' " other stuff"
##' return(FALSE)
##' }
##' }
foo <- function(x, y, z){
rnorm(10) == 1
.NotYetImplemented()
" other stuff"
return(FALSE)
}
于 2013-03-04T00:22:05.493 回答
0
请参阅此相关问题。没有全局标志或解决方案。@baptiste's 是最好的。答案设置为社区 wiki,以防这种情况发生变化。
于 2013-03-05T07:15:34.793 回答