It looks like a parameterized report might be what you need. See the yaml more details, but the main idea is that you set the parameter in yaml your yaml report and use this parameter in the report to configure it (for example, by filtering data on City in your case). Then, in a separate R script, you render report several times, once for each City value that you pass as a parameter to the render function. Here is a basic example:
In the rmarkdown report rmarkdown you should declare the parameter in yaml . The enumerated Dallas value in this case is the default value if no other value is entered when rendering the report:
--- title: My Document output: pdf_document params: My_City: Dallas ---
Then in the same rmarkdown document you will have the whole report - no matter what calculations depend on City , plus a template that is the same for any City . You access the parameter using params$My_City . The code below will filter the data frame for 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 create a separate report for each City (where I assumed that 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 above code, I assumed that the dat data frame is in the global environment of this separate R script that displays MyReport.Rmd . However, you can also simply specify a vector of city names instead of getting names from unique(dat$City) .
source share