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.

3 Answers 3

5
+50

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

Sign up to request clarification or add additional context in comments.

3 Comments

You might take a look at knit_expand as an alternative to knit_child.
You can simplify the start of stepfile.Rhtml to <h2><!--rinline i--> is a great number</h2> (using inline evaluation).
Using I(paste(out, collapse = '\n')) made it work. This removed the wrapper and now knirtBootstrap displays it nicely. Thank you very much!
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?

2 Comments

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.
I understand... Maybe there's a way with child document, see answer from Hugo Koopmans in stackoverflow.com/questions/11956520/…
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-->

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.