42010

Knitr HTML Loop - Some HTML output, some R output

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.

Answer1:

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

Answer2:

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?

Answer3:

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-->

Recommend

  • How to debug “contrasts can be applied only to factors with 2 or more levels” error?
  • R{ff}:How to add a new column which depends on other elements in the same row in ffdf object?
  • Unable to compile AdaControl: unknown project file: “asis”
  • In the “Tables”-package: How to get column percentages of a subset of a variable?
  • how to insert data into multiple tables through ItemWriter
  • Split an image into 64x64 chunks
  • several hexbin plots on one PDF page in R
  • R ggvis interactive slider for calculating y values (e.g. for background correction)
  • Spacing/Leading PdfPCell's elements
  • PyRun_String stop sending result to stdout after any error
  • How to combine two lists together?
  • What is wrong with this emulation of CMPXCHG16B instruction?
  • Django Migrations fail during django initialization
  • How to model a mixture of finite components from different parametric families with JAGS?
  • Sending rails errors to rspec output
  • SSH in Bash Script Messing Up File Read
  • Yii2: Using Kartik Depdrop Widget?
  • How to determine the CCSID used in CPYFRMIMPF command?
  • Serve file to user over http via php
  • php is_file always return false
  • Geom_jitter colour based on values
  • Lua: Line breaks in strings
  • Wrapping a c#/WPF GUI around c++/cli around native c++
  • Why doesnt this Java loop in a thread work?
  • Changing Jupyter Notebook start up folder by modifying “start in” not working any more
  • Randomizing -and remembering that randomisation- multiple choice questions in php
  • Julia: How to give multiple workers access to functions that are 'include(…)' into a modul
  • Sending keystrokes/mouse clicks to a Java program with Autohotkey
  • How can I display the parent menu item's description using Wordpress walkers?
  • Button text different than value submitted in query string
  • MySQL Order by column = x, column asc?
  • Is playing sound in Javascript performance heavy?
  • Date Conversion from yyyy-mm-dd to dd-mm-yyyy
  • x64 applications using gdi+: what are the consequences on performance?
  • How to match http request and response using Jersey ContainerRequestFilter and ContainerResponseFilt
  • Insert into database using onclick function
  • Different response to non-authenticated users and AJAX calls
  • Knitr HTML Loop - Some HTML output, some R output
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Benchmarking RAM performance - UWP and C#