2

I want to loop through a list and and print some part of it in HTML and some as Code. So be more precise: I want to produce the same output this is creating

<h2> 1 is a great number </h2> 
<!--begin.rcode echo=FALSE print(rnorm(5,mean=1)) end.rcode--> 
<h2> 2 is a great number </h2> 
<!--begin.rcode echo=FALSE print(rnorm(5,mean=2)) end.rcode-->
...
<h2> x is a great number </h2> 

I managed to print the 's to HTML but the results are printed directly in HTML as well, with the following Chunk:

<!--begin.rcode, echo=FALSE, results = 'asis'
for (i in list(1,2)){
   cat("<h2>", i, "is a great number</h2>")
   print(rnorm(5,mean=i))
}
end.rcode-->

Would be very happy about all suggestions.

P.S.: The reason why I want to have the formatting is that knirtBootstrap, then produces a very nice Output.

micstr
  • 5,080
  • 8
  • 48
  • 76
Rentrop
  • 20,979
  • 10
  • 72
  • 100

3 Answers3

5

Hello again Floo0 an other solution using two .Rhtml files. The first one, mainfile.Rhtml, calls the second one as many time you want. In stepfile.Rhtml you can put chunks as you want. You just have to compile mainfile.Rhtml.

## mainfile.Rhtml

<!--begin.rcode echo=FALSE
J <- 10
end.rcode-->


<!--begin.rcode include=FALSE
out <- NULL
for (i in 1:J) {
  out <- c(out, knit_child('stepfile.Rhtml'))
}
end.rcode-->


<!--rinline paste(out, collapse = '\n') -->


## stepfile.Rhtml

<!--begin.rcode echo=FALSE, results='asis'
cat("<h2>", i, "is a great number</h2>")
end.rcode-->

<!--begin.rcode echo=FALSE
print(rnorm(5,mean=i))
end.rcode-->

I took the idea from Dynamic number of calls to a chunk with knitr

Community
  • 1
  • 1
Victorp
  • 13,636
  • 2
  • 51
  • 55
  • You might take a look at knit_expand as an alternative to knit_child. – Thell Mar 02 '14 at 00:28
  • 1
    You can simplify the start of stepfile.Rhtml to `

    is a great number

    ` (using inline evaluation).
    – Brian Diggs Mar 04 '14 at 02:15
  • Using `I(paste(out, collapse = '\n'))` made it work. This removed the wrapper and now `knirtBootstrap` displays it nicely. Thank you very much! – Rentrop Mar 06 '14 at 17:47
1

With something like this :

<!--begin.rcode, echo=FALSE, results = 'asis'
for (i in list(1,2)){
   cat("<h2>", i, "is a great number</h2>")
   cat("</pre></div>")
   cat("<div class='output'><pre class='knitr r'>")
   cat("## ")
   print(rnorm(5,mean=i))
   cat("</pre></div>")
}
end.rcode-->

Does it help?

Victorp
  • 13,636
  • 2
  • 51
  • 55
  • 1
    Hello Victorp, thanks for your Answer. I thought about a solution like this as well, but this kind of makes knitr nonsence. This way you start writing all the HTML environments yourself... Exactly the job that knitr should do for you. – Rentrop Feb 18 '14 at 15:41
  • I understand... Maybe there's a way with child document, see answer from Hugo Koopmans in http://stackoverflow.com/questions/11956520/r-knitr-markdown-output-plots-within-for-loop/17105758#17105758 – Victorp Feb 20 '14 at 08:35
0

I think this is a bad hack but you can do:

<!-- begin.rcode setup, include=FALSE
tmpl <- '<!-- begin.rcode tmpl-label-%d,
print(rnorm(5,mean=i))
\nend.rcode-->'
end.rcode-->

<!--begin.rcode echo=FALSE, results='asis'
for (i in 1:2) {
  cat("<h2>", i, "is a great number</h2>")
  cat(knit(text=sprintf(tmpl, i), quiet=TRUE))
}
end.rcode-->
kohske
  • 65,572
  • 8
  • 165
  • 155