User Tools

Site Tools


r-brew

Extensions to the R brew Package

Literal Evaluation

The code below may be passed as the tplParser argument to the brew function in the brew package by Jeff Horner. brewLiteral does something similar to Sweave, by echoing the evaluated code, and printing the results.

brewLiteral.R
brewLiteral <- function(code) {
    out <- ""
    exp <- try(parse(text=code),TRUE)
    if(inherits(exp, "try-error"))
        return(exp)
    for(i in 1:length(exp)) {
        dep <- deparse(exp[[i]])
        for(j in 1:length(dep)) {
            pmt <- ifelse(j > 1, 
                getOption("continue"),
                getOption("prompt"))
            out <- paste(out, pmt, dep[j], "\n", sep="")
        }
	res <- try(capture.output(eval(exp[i])),TRUE)
	if(length(res)>0 && !grepl("^.*\\n$", res))
            res <- paste(res, "\n")
        out <- paste(out, res, sep="")
    }
    return(out)
}

Problems

  • tplParser is not evaluated in the cumulative brew environment
  • brew doesn't always close the input connection (Warning message: closing unused connection)

Ideas

  • allow brew to accept additional, or generic delimiters of the form '<%?' and '%>'
  • allow the user to specify actions for additional delimiters a la tplParser
r-brew.txt · Last modified: 2017/09/20 19:05 (external edit)