Reputation: 91
How do I create multiple HTML files in a single R markdown file with the Knit HTML option in RStudio? I want to create a separate HTML file with graphs for different levels of a variable in a data frame. I know a similar question has been posted in How to best generate multiple HTML files from RMarkdown based on one dataset?, but I do not manage to get this running.
Could someone give me a simple example, for instance to create 3 HTML files of the mtcars dataset for each amount of cylinders.
data(mtcars)
for(cyl in unique(mtcars$cyl)){
plot.data <- mtcars[mtcars$cyl==cyl,]
plot(plot.data$mpg,plot.data$disp,main=paste(cyl,"cylinders"))
}
This would be of great help for me!
Thanks in advance, Gijs
Upvotes: 1
Views: 6125
Reputation: 91
I managed to do the job!
The answer to the post R Knitr PDF: Is there a posssibility to automatically save PDF reports (generated from .Rmd) through a loop? really helped me.
The render function was the solution.
I thought I had to run this type of markdown just through the Knit HTML button, but then only one HTML file is created. The render function can be used in a for loop in R, calling a template.Rmd file to create multiple HTML files.
Upvotes: 2