Table of Contents

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

Ideas