我很清楚当代码直接在roxygen注释中时,如何使roxygen不运行示例。但是,某些示例可能有些冗长,或者您希望将示例编译在examples目录中。在这种情况下@example file_path工作正常,但我无法弄清楚如何使roxygen无法运行(即\dontrun)示例文件。
承认这与该问题非常相似,但注释显示该问题未得到回答。
测试
# this does not work
#' @title test_fun
#' @example \dontrun{examples/test_example.R}
test <- function(){
print("hello")
}
# this does
#' @title test
#' @examples
#' \dontrun{
#' test()
#' }
test <- function(){
print("hello")
}
Run Code Online (Sandbox Code Playgroud)
test_example.R
test()
Run Code Online (Sandbox Code Playgroud)
如何获得前一种方法?
看来我可以通过对\dontrun{}示例文件中的块使用 roxygen2 风格的注释来完成此任务。这绕过了Michal 答案中的限制。
创建一个如下所示的示例文件:
#' \dontrun{
test()
#' }
Run Code Online (Sandbox Code Playgroud)
更可靠的是,您可以将示例包装在一个if(interactive()) {}块中,该块在检查期间不会运行,但允许您手动运行示例。