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 <- 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) }
tplParser
is not evaluated in the cumulative brew
environmentbrew
doesn't always close the input connection (Warning message: closing unused connection)tplParser