I have (re)implemented some features of knitr
independently from @Yihui based on brew
in my pander package that could help with such (and similar) issues if you do not want to run brew
before knit
ting. Quick demo:
> Pandoc.brew(text = "# Demonstrating a nice loop
+ <% for (i in 1:5) { %>
+ ## This is a header for <%=i%>
+ #' This is a comment for <%=i%>
+ <% } %>")
# Demonstrating a nice loop
## This is a header for _1_
#' This is a comment for _1_
## This is a header for _2_
#' This is a comment for _2_
## This is a header for _3_
#' This is a comment for _3_
## This is a header for _4_
#' This is a comment for _4_
## This is a header for _5_
#' This is a comment for _5_
Please note that you could also pass a file to Pandoc.brew
(no need to use such troublesome setup with the text
argument with real-life problems), and that you could also use <% ... %>
tags for e.g. conditionals (like showing or not rendering part of a report). And most importantly: there is a huge difference between <% ... %>
(unprocessed R commands) and <%= ... %>
(results are processed by pander
) tags. The latter means that all returned R objects are transformed to Pandoc's markdown, e.g.:
> Pandoc.brew(text = "# Demonstrating a conditional
+ <% for (i in 1:5) { %>
+ ## This is a header for <%=i%>
+ <% if (i == 3) { %>
+ Hey, that's **almost** <%=pi%>, that's between <%=3:4%>! Wanna fit a model to _celebrate_?
+ <%= lm(mpg ~ hp, mtcars) %>
+ <% }} %>")
# Demonstrating a conditional
## This is a header for _1_
## This is a header for _2_
## This is a header for _3_
Hey, that's **almost** _3.142_, that's between _3_ and _4_! Wanna fit a model to _celebrate_?
--------------------------------------------------------------
Estimate Std. Error t value Pr(>|t|)
----------------- ---------- ------------ --------- ----------
**hp** -0.06823 0.01012 -6.742 1.788e-07
**(Intercept)** 30.1 1.634 18.42 6.643e-18
--------------------------------------------------------------
Table: Fitting linear model: mpg ~ hp
## This is a header for _4_
## This is a header for _5_
brew
is more sophisticated for this kind of tasks (generate text from a loop). – Incendiary