roxygen2 how to not run example file

浪尽此生 提交于 2019-12-23 11:51:49

问题


I am well aware of how to have roxygen not run an example when the code is directly in the roxygen comments. However, some example may be somewhat verbose or you would want you examples compiled in an examples directory. In which case the @example file_path works fine but I can't figure out how to have roxygen not run (i.e. \dontrun) the example file.

This is admitted very similar to this question but the comments show that this problem was not answered.

test.R

# 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")
}

test_example.R

test()

How can I get the former approach to work?


回答1:


It seems I'm able to accomplish this by using roxygen2-style comments for the \dontrun{} block in the example file. This gets around the limitation in Michal's answer.

Create an example file that looks like this:

#' \dontrun{
test()
#' }

More reliably, you can wrap your example in an if(interactive()) {} block, which won't be run during checks but allows you to manually run through the example.




回答2:


I think it works if you just add \dontrun to the example script, so your test_example.R would look like:

\dontrun{
test()
}

Even though now the example script cannot be sourced directly as \dontrun is not an R expression...



来源:https://stackoverflow.com/questions/31412724/roxygen2-how-to-not-run-example-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!