These functions provide a cache system for brew templates.

brewCache(envir=NULL)

brewCacheOn()
brewCacheOff()

setBufLen(len=0)

Arguments

envir

the environment to store text and R expressions for each brewed template.

len

length of internal buffers for parsing the templates.

Details

brew can cache parsed templates for potential speedup but only when brew calls are passed filenames, not connections, and when tplParser is NULL.

brew caching is implemented by storing file names, modification times, and the associated text and R expressions in an internal environment. Calling brewCache() with an appropriate environment sets the internal cache. Calling without arguments returns the internal cache. The cache is exposed this way mainly for testing, debugging, performance improvement, etc. and this may be off-limits in future releases.

Simple enough, brewCacheOn() turns on caching of brew templates and is equivalent to calling brewCache(envir=new.env(hash=TRUE,parent=globalenv())). brewCache() without arguments returns the internal environment. Calling brewCacheOff() turns off caching by setting the internal environment to NULL.

One thing to note: filenames act as keys in the internal cache environment, and brew does nothing to expand them to their full paths. Thus, '/home/user/brew.html' and '~usr/brew.html' will act as separate keys, although on-disk they may actually point to the same file.

setBufLen() initializes internal parsing vectors to length len. Default is 0.

Value

brewCache() without arguments returns the internal cache. All others invisibly return NULL.

Author

Jeffrey Horner <jeff.horner@vanderbilt.edu>

See also

Sweave for the original report generator.

Examples


## Turn on caching
brewCacheOn()

## Brew a template
brew(system.file("featurefull.brew",package="brew"),envir=new.env())
#> You won't see this R output, but it will run. 
#> Now foo is bar and today is October 14, 2025.
#> How about generating a template from a template?
#> <% 
#> 	foo <- 'fee fi fo fum' 
#> 	brew <- 'haha'
#> %>
#> foo is still bar.
#> Contents of current directory:
#> [1] "brew.html"  "index.html"
#> 

## Get the internal cache
cache <- brewCache()

## Inspect
as.list(cache)
#> $`/tmp/RtmpzgcSH2/temp_libpath3bbc1415ec28fc/brew/featurefull.brew`
#> $`/tmp/RtmpzgcSH2/temp_libpath3bbc1415ec28fc/brew/featurefull.brew`$mtime
#> [1] "2025-10-14 14:54:50 UTC"
#> 
#> $`/tmp/RtmpzgcSH2/temp_libpath3bbc1415ec28fc/brew/featurefull.brew`$env
#> <environment: 0x5569039c30b0>
#> 
#> 

## Inspect first file cached in list
as.list(cache)[[1]]
#> $mtime
#> [1] "2025-10-14 14:54:50 UTC"
#> 
#> $env
#> <environment: 0x5569039c30b0>
#> 

## Inspect environment that contains text and parsed code
as.list(as.list(cache)[[1]]$env)
#> $code
#> expression(.brew.cat(1, 1), foo <- "bar", .brew.cat(3, 4), cat(foo), 
#>     .brew.cat(6, 6), cat(format(Sys.time(), "%B %d, %Y")), .brew.cat(8, 
#>         8), .brew.cat(13, 20), cat(foo), .brew.cat(22, 23), print(dir()), 
#>     .brew.cat(25, 25))
#> 
#> $text
#>  [1] "You won't see this R output, but it will run. "    
#>  [2] " foo <- 'bar' "                                    
#>  [3] "\n"                                                
#>  [4] "Now foo is "                                       
#>  [5] "foo"                                               
#>  [6] " and today is "                                    
#>  [7] "format(Sys.time(),'%B %d, %Y')"                    
#>  [8] ".\n"                                               
#>  [9] "# Comment -- ignored -- useful in testing. \n"     
#> [10] "    Also notice the dash-percent-gt.\n"            
#> [11] "    It chops off the trailing newline. \n"         
#> [12] "    You can add it to any percent-gt. "            
#> [13] "How about generating a template from a template?\n"
#> [14] "<%"                                                
#> [15] " \n"                                               
#> [16] "\tfoo <- 'fee fi fo fum' \n"                       
#> [17] "\tbrew <- 'haha'\n"                                
#> [18] "%>"                                                
#> [19] "\n"                                                
#> [20] "foo is still "                                     
#> [21] "foo"                                               
#> [22] ".\n"                                               
#> [23] "Contents of current directory:\n"                  
#> [24] " print(dir()) "                                    
#> [25] "\n"                                                
#> 

## Turn off brew caching
brewCacheOff()
rm(cache)