<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/many-reports-from-1-rmarkdown-file/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://scottishsnow.wordpress.com/2018/08/17/many-reports-from-1-rmarkdown-file/"> R – scottishsnow</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I was at the EdinbR talk this week by the RStudio community lead – Curtis Kephart. It was really interesting, but I disagree with his suggestion to point and click different parameters when you want to generate multiple reports from the same RMarkdown file. This might be acceptable if you have one or two, but any more and the chance for error and tedium is greatly increased. This blog post shows you how to loop (yes – an actual for loop!) through a variable to generate different reports for each of its unique values.</p>
<p>First, we need an RMarkdown file (.Rmd). This is largely the same as your usual .Rmd file, and I strongly encourage you to develop it like one. i.e. write your single .Rmd file and convert it into a special use case to be a template. Working like this makes debugging a whole lot easier. Here’s an example of a “normal” .Rmd:</p>
<pre>
---
title: &quot;Demographics exploratory analysis&quot;
author: &quot;Mike Spencer&quot;
date: &quot;11 September 2017&quot;
output:
  pdf_document:
    toc: yes
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo=F, message=F, results='hide', warning=F, fig.height=9)
source(&quot;read.R&quot;)
library(tidyverse)
library(RColorBrewer)
library(knitr)
```

## Introduction

This file provides a summary of the digital potential in the rural UK survey.
In particular it shows the answers for females.

The summary had `r nrow(df)` responses, of these `r sum(is.na(df$Eastings))` either had no postcode or the postcode supplied did not match the current list from Ordnance Survey or the UK data service (Northern Irish postcodes).
Those postcodes which were matchable could be related to country and their rural-urban classification.
</pre>
<p>You can see it’s not far off what you get when you opt to start a new RMarkdown file in RStudio. I’ve abstracted the data reading to a separate file (it has some lengthy factor cleaning and is used in a few different situations), and I’m loading the knitr library so I can make tables with kable().</p>
<p>The next code chunk shows how the file is adapted to be used as a template for many outputs:</p>
<pre>
---
params:
   new_title: &quot;My Title!&quot;
title: &quot;`r params$new_title`&quot;
author: &quot;Mike Spencer&quot;
date: &quot;11 September 2017&quot;
output:
  html_document:
    toc: yes
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo=F, message=F, results='hide', warning=F, fig.height=9)
library(tidyverse)
library(RColorBrewer)
library(knitr)

df1 = df %>% filter(gender==v)
df1 = droplevels(df1)
```

## Introduction

This file provides a summary of the digital potential in the rural UK survey.
In particular it shows the answers of `r v`.

The summary had `r nrow(df1)` responses, of these `r sum(is.na(df1$Eastings))` either had no postcode or the postcode supplied did not match the current list from Ordnance Survey or the UK data service (Northern Irish postcodes).
Those postcodes which were matchable could be related to country and their rural-urban classification.
</pre>
<p>Pretty similar, but there are some subtle differences. We’re now passing a title parameter to our .Rmd, our data are already loaded and we subset them to df1. In reality, this second step could happen in the next file – choose your preference for readability (and whether you want to change all the df variables in your .Rmd to df1).</p>
<p>Finally, we need a separate script to loop through our variable and make some reports!</p>
<pre>
library(&quot;rmarkdown&quot;)

source(&quot;~/repo/read.R&quot;)

slices = unique(df$gender)

for(v in slices){
  render(&quot;~/repo/exploratory_template.Rmd&quot;,
         output_file=paste0(&quot;~/results/exploratory_&quot;, v, &quot;.html&quot;),
         params=list(new_title=paste(&quot;Exploratory analysis -&quot;, v)))
}
</pre>
<p>Note we’re explicitly loading the rmarkdown library here so we can use the render function. We’re also loading our data before our loop, to speed our code up. The object v is passed to the .Rmd file, which is what we use to subset our data.</p>
<p>If you’ve made it this far you should now have the tools to make multiple reports with a lot less effort! Beware that it becomes very easy to make more outputs than anyone could possibly read – with great power comes etc, etc..</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://scottishsnow.wordpress.com/2018/08/17/many-reports-from-1-rmarkdown-file/"> R – scottishsnow</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/8pkVd6yE-Ao" height="1"></a></div>