Question

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?

Was it helpful?

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

OTHER TIPS

See this related question. There is no global flag or solution. @baptiste's is as good as it gets. Answer set to community wiki in case this state of affairs changes.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top