I have a dataset that looks like
City Score Count Returns
Dallas 2.9 61 21
Phoenix 2.6 52 14
Milwaukee 1.7 38 7
Chicago 1.2 95 16
Phoe
It looks like a parameterized report might be what you need. See the link for details, but the basic idea is that you set a parameter in the yaml
of your rmarkdown
report and use that parameter within the report to customize it (for example, by filtering the data by City
in your case). Then in a separate R script, you render
the report multiple times, once for each value of City
, which you pass as a parameter to the render
function. Here's a basic example:
In your Rmarkdown
report you would declare the parameter in the yaml
. The listed value, Dallas
in this case, is just the default value if no other value is input when you render the report:
---
title: My Document
output: pdf_document
params:
My_City: Dallas
---
Then, in the same Rmarkdown
document you would have your entire report--whatever calculations depend on City
, plus the boilerplate that's the same for any City
. You access the parameter with params$My_City
. The code below will filter the data frame to the current value of the My_City
parameter:
```{r}
dat %>%
filter(City==params$My_City) %>%
summarise(Score = median(Score),
Count = mean(Count) ,
Return= mean(Returns))
```
Then, in a separate R script, you would do something like the following to produce a separate report for each City
(where I've assumed the Rmarkdown file above is called MyReport.Rmd
):
for (i in unique(dat$City)) {
rmarkdown::render("MyReport.Rmd",
params = list(My_City = i),
output_file=paste0(i, ".pdf"))
}
In the code above, I've assumed the dat
data frame is in the global environment of this separate R script that renders MyReport.Rmd
. However, you could also just provide a vector of city names instead of getting the names from unique(dat$City)
.