Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I often find myself performing analyses looped over certain parts of the data in my Rmarkdown. I like seeing the output under different headings in the final output for easy navigation. Although it is rather simple, I can never remember the recipe for this right away, and as a result, I keep digging into old rmd files and the all-knowing internet. I decided to write this down once and for all to break the cycle and save my future self some time and prevent moments like this:

Points to remember:

• Set chunk option results="asis"
• Create headings using cat() function in loop
• Call print() on the code to generate desired output (tables, plots, code output, etc)
• Wrap the output within appropriate HTML tags
• For DT::datatable outputs initiate dependencies previously and use htmltools::tagList()
• For neatly rendered console outputs, wrap the code with htmltools::pre() and htmltools::code().

1. Example for creating tables in loop:

I have run into an issue before where tables printed in loop don’t render in the final document. As suggested in this SO post creating an initial datatable output is needed to load some magic dependencies. To hide this chunk from the final output though, you can set chunk option include=FALSE:

{r, include=FALSE}

library(DT)

datatable(iris)




The chunk below will create new headings with the names within the list (e.g. data1, data2, and data3) and show a datatable. Check out the documentation to further customize the table (filters, conditional formatting, captions are a few things that make the tables more useful).

{r, results="asis"}

library(htmltools)

list_to_output <- list(data1 = iris,
data2 = mtcars,
data3 = airquality)

for(i in names(list_to_output)){

cat("\n")
cat("##", i, "\n") # Create second level headings with the names.

print(
tagList(
datatable(list_to_output[[i]])
)
)

cat("\n")

}




2. Example for creating console output:

We need to wrap the code output in

 and  tags for rendering properly. Without these the console output will render like normal text. This is especially helpful if you are trying to print named vectors where the name and the value will be aligned neatly.
{r, results="asis"}

for(i in paste("Output", 1:3)){

cat('\n')
cat("###", i, "\n")

vec <- 1:10

pre(       # can use tags manually as well: BlaBlaBla
code(
print(vec)
)
)

}


3. Example for creating plot output:
This is one is more straightforward since we don’t need to deal with HTML tags specifically
{r, fig.width=3, fig.height=3, results="asis"}

for(i in paste("Plot", 1:3)){

cat('\n')
cat("###", i, "\n")

print(
ggplot(iris) +
geom_point(aes(x=Sepal.Length, y=Petal.Length))
)

}

var vglnk = {key: '949efb41171ac6ec1bf7f206d57e90b8'};
(function(d, t) {
var s = d.createElement(t);
s.type = 'text/javascript';
s.async = true;
// s.defer = true;
var r = d.getElementsByTagName(t)[0];
r.parentNode.insertBefore(s, r);
}(document, 'script'));

Related
ShareTweet

`