Search code examples
rroxygenliterate-programmingroxygen2

Is there a global flag that can be set to provide code alongside ROxygen documentation?


I have taught a class in which I have had students use a package I have written. Now that the class is ending, I'd like to provide them the code for each of those functions inline with the documentation for the functions. Is there a global flag I can set to accomplish this? A code hack of some kind?


Solution

  • You could pre-process your R files with the brew package, e.g.

    File '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)
      }
    

    Then process the file to generate foo.r

    source("foo-tmp.r") # to know what the function is
    brew("foo-tmp.r", "foo.r")
    

    with resulting output:

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