# [R] Template Engine for R

Henrik Bengtsson henrik.bengtsson at ucsf.edu
Sat May 16 17:17:47 CEST 2015

Hi Luca,

I must admit I've not used template engines like the ones you refer
to, but I can guess the idea and I believe RSP (the name of the markup
language defined in R.rsp) is powerful enough to achieve something similar.

RSP, has two main sets of constructors: (a) RSP pre-processing
directives and (b) RSP code expressions.

The pre-processing directives are independent of the R language, e.g.
<@%include file="<pathname>|<url>"%> and <@%ifeq a="42"%> ...
<@%else%> ... <@%endif%>.  They would look/work the same if, say,
Python implemented an RSP engine.

The code expressions contain R code, e.g. <% x <- runif(10) %> and <%=
x %>.  These are specific to R.  If implemented in Python, these would
contain Python code.  (There's a concept/framework for
mix-and-matching languages too).

There is no direct way of using the language-independent
pre-processing directives to setup templates.  However, you can do it
use the the RSP code expressions.  The following example is from the
http://cran.r-project.org/web/packages/R.rsp/vignettes/Dynamic_document_creation_using_RSP.pdf
vignette:

<% myTemplate <- function(n, ...) { %>
The sum of $x=<%=hpaste(1:n, abbreviate="\\ldots")%>$ is <%=sum(1:n)-%>.<%-%>
<% } # myTemplate() %>

\begin{itemize}
<% for (ii in c(3,5,10,100)) { %>
\item <% myTemplate(n=ii) %>
<% } # for (ii ...) %>
\end{itemize}

You can of course define the myTemplate() function in a separate file
and reuse it by <@%include file="templates.rsp"%>.  It can also
contain pre-processing directives.  It can in turn include other RSP
files.

Finally, I'm not sure what your target output format is, but RSP is
designed to work with _any text-based formats_, so you can use it to
generate code ('script.bash.rsp'), tab-delimited text files
('genes.tsv.rsp') and so on.  For example:

# A data frame save from R
# created_by: <%= author %>
# created_on: <%= Sys.date() %>
# nbr_of_rows: <%= nrow(data) %>
# column_names: <%= paste(sQuote(colnames(data)), collapse=", ") #>
# column_classes: <%= paste(sQuote(sapply(data, typeof)), collapse=", ") #>
<% write.table(data, sep="\t", quote=FALSE) %>

will output a tab-delimited table with header comments.  Running it through as

data <- ...
tsv <- rfile("genes.tsv.rsp")

will generate file 'genes.tsv'.  If you want the output to be sent to
stdout, you can do rcat(file="genes.tsv.rsp").

/Henrik

On Sat, May 16, 2015 at 3:57 AM, Luca Cerone <luca.cerone at gmail.com> wrote:
> Thanks Henrik!
>
> That's seem more like what I am looking for, though I do not have to
> use a template engine for reporting purposes.
> I do not need to create html, nor markdown but I'll see if I can use
> it for what I need :)
>
> Cheers,
> luca
>
> On Sat, May 16, 2015 at 9:41 AM, Henrik Bengtsson
> <henrik.bengtsson at ucsf.edu> wrote:
>> See http://cran.r-project.org/package=R.rsp
>>
>> Henrik
>> (author)
>>
>> On Fri, May 15, 2015 at 11:34 PM, Luca Cerone <luca.cerone at gmail.com> wrote:
>>> Dear all,
>>> I am looking for a template engine for R.
>>>
>>> I have already come across {{mustache}} and its R implementation whisker,
>>> however I am looking for something with a few more features like "if blocks",
>>> "for loop", "block inheritance" and so on (for those of you who
>>> are familiar with Python I am looking for something like Jinja2 or Mako).
>>>
>>> I searched in google, but the only option for R seems whisker.
>>>
>>> Can any of you recommend me some alternatives?
>>>
>>> Thanks a lot in advance for the help!
>>>
>>> Cheers,
>>> Luca
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat.ethz.ch/mailman/listinfo/r-help