Skip to content

Template Engine for R

9 messages · Jeff Newmiller, Henrik Bengtsson, Federman, Douglas +1 more

#
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
#
Not familiar with your examples, but knitr relies on R for control flow and whisker for template substitution, so it kind of seems unnecessary to have yet another syntax for control flow.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.
On May 15, 2015 11:34:44 PM PDT, Luca Cerone <luca.cerone at gmail.com> wrote:
#
Hi Jeff,
thanks for your reply.

Can you elaborate a bit more what you mean? I know very lillte knitr,
mostly because I use it in Rstudio to render Rmarkdown documents.
However I do not need a template engine for reporting, and I can't see
how I should use it.

Can you provide me with  just a minimal example?

Cheers,
Luca

On Sat, May 16, 2015 at 9:11 AM, Jeff Newmiller
<jdnewmil at dcn.davis.ca.us> 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:
#
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:
#
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:
#
On Sat, May 16, 2015 at 5:17 PM, Henrik Bengtsson
<henrik.bengtsson at ucsf.edu> wrote:
That's exactly what I am looking for then :)

I have to create a set of .txt templates. 90% of the time I just will
have to do simple "tags" substitutions,
but there are some times where I'll need some control flow.

Thanks again for linking me this!

Cheers,
Luca
2 days later
#
There is a new one on CRAN called infuser that may meet your needs


--
One who is in a dying condition is regarded as a living person in all respects.
    -- Maimonides


-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Luca Cerone
Sent: Saturday, May 16, 2015 2:35 AM
To: R-help
Subject: [R] Template Engine for R

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
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
#
Hi Douglas,
I read about it just this morning!
It seems interesting, but I don't think it handles if and for, yet.

I'll look at it in more details, though.

Thanks a lot for hint :)

Cheers,
Luca


On Mon, May 18, 2015 at 8:45 PM, Federman, Douglas
<Douglas.Federman at utoledo.edu> wrote: