brew {brew}R Documentation

Report Brewing For Text and R Output


brew provides a templating system for text reporting. The syntax is similar to PHP, Java Server Pages, Ruby's erb module, and Python's psp module.


     run=TRUE,parseCode=TRUE,tplParser=NULL,chdir=FALSE, extendedErrorReport=FALSE)



A connection, or a character string naming the file to read from. stdin() is the default.


A connection, or a character string naming the file to print to. stdout() is the default.


A character string treated as if it contained lines of a file to read from. Only one of file or text is used as input. Default is NULL.


the environment in which the input is to be evaluated. Default is the caller's environment, useful for nested brew calls.


Logical to determine if brew should evaluate the input (run=TRUE) or just parse it (run=FALSE). Useful for debugging.


Logical. only relevant when run=FALSE. When TRUE the brewed code is parsed and then silently returned. When FALSE, the brewed code is returned as a list. See the Value section for details.


a function to parse the text between '<%%' and '%%>' and return the result as a character vector. The template text is passed to the function as a variable length character vector in the first argument position.


logical; if TRUE and file is a pathname, the R working directory is temporarily changed to the directory containing file for evaluating. brew will also honor the global option brew.chdir.


changes error handling behaviour to print a stack trace when an error occurs, the global option brew.extended.error can also be used to achive the same effect. Existing brew behaviour is preserved if this switch is not set to TRUE.


brew syntax is quite simple and there are very few delimiters to learn:

The following template contains syntax to exercise all brew functionality:

You won't see this R output, but it will run. <% foo <- 'bar' %>
Now foo is <%=foo%> and today is <%=format(Sys.time(),'%B %d, %Y')%>.
<%# Comment -- ignored -- useful in testing. 
    Also notice the dash-percent-gt.
    It chops off the trailing newline. 
    You can add it to any percent-gt. -%>
How about generating a template from a template?
<%% foo <- 'fee fi fo fum' %%>
foo is still <%=foo%>.

The output is:

You won't see this R output, but it will run.
Now foo is bar and today is April 20, 2007.
How about generating a template from a template?
<% foo <- 'fee fi fo fum' %>
foo is still bar.

Also, for power users, there's one more thing:

NOTE: brew calls can be nested and rely on placing a function named '' in the environment in which it is passed. Each time brew is called, a check for the existence of this function is made. If it exists, then it is replaced with a new copy that is lexically scoped to the current brew frame. Once the brew call is done, the function is replaced with the previous function. The function is finally removed from the environment once all brew calls return.


When run=TRUE, the value of the last expression after brewing the input or an object of class 'try-error' containing the error message if brewing failed.

When run=FALSE and parseCode=TRUE, a function whose environment contains the text vector and the code vector of the parsed expressions after brewing the input. It takes brew's output and envir arguments.

When run=FALSE and parseCode=FALSE, a list containing the text vector and the unparsed code vector.


Jeffrey Horner <>

See Also

Sweave for the original report generator.


## A port of the Sweave test file.
##clean up generated files

## Everything you wanted to know about your R session.

## clean up generated files

## Don't sully up environment, so use envir=new.env(). Nested brew calls will still work.

## Various ways to print R output 

## The example from the Details section

## Using the tplParser argument
tParse <- function(text) paste('Got this: <',text,'>\n',sep='',collapse='')

[Package brew version 1.0-10 Index]