Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to toggle roxygen comments in Rstudio?

Roxygen comments involve prefixing lines with #'. When writing and testing examples for functions, it's nice to be able to toggle the comments on and off. I could copy and paste the code back and forward to vim and remove or add those comments, but that's not very elegant.

  • Is there any easy way to toggle roxygen comments in Rstudio?
  • Alternatively, is there another way to efficiently run example R code that is commented out by roxygen comment characters?

Update: Thinking laterally, I suppose using @example examples/foo.r is an alternative way of avoiding having to use Roxygen comments for the actual example code (i.e., by sourcing the example from a file, i.e., examples/foo.r).

like image 788
Jeromy Anglim Avatar asked Oct 05 '13 08:10

Jeromy Anglim


1 Answers

With respect to your proposed alternative:

  • Alternatively, is there another way to efficiently run example R code that is commented out by roxygen comment characters?

If you press CTRL+[Enter] within a Roxygen2 @examples block, Rstudio will send the selected code (line or highlighted section) to the R console. To use just declare the @examples code block on a line preceding your roxygen commented code.

#' @examples
#'   ... your original roxygen commented code ...

You can put an @examples block anywhere in your code. This becomes a drag if you are developing a package and you are using the block for its intended purpose.

If you are looking for a way to toggle code, I would use the approach proposed by @Roman in in the comments to your question.

like image 63
ctbrown Avatar answered Oct 21 '22 00:10

ctbrown