6

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
 Phoenix    5.9 96  16
 Dallas 1.9 45  12
 Dallas 2.7 75  45
 Chicago    2.2 75  10
 Milwaukee  2.6 12  2
 Milwaukee  4.5 32  0
 Dallas 1.9 65  12
 Chicago    4.9 95  13
 Chicago    5   45  5
 Phoenix    5.2 43  5

I would like to build a report using R markdown; however, for each city I need to build a report. The reason for this is that one city cannot see the report for another city. How do I build a report and save a PDF of it for each city?

Each report would need the median Score, mean Count, and mean Returns. I know that using dplyr I could simply use

finaldat <- dat %>%
            group_by(City) %>%
            summarise(Score = median(Score),
                      Count = mean(Count)  ,
                      Return= mean(Returns))

But the frustration comes from producing a report for each City. Also, this is a subset of the data, not the full data. That is, this report is extensive and is a report of the results, which is systematic, not different for each City.

0

1 Answer 1

13

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).

To use a dynamic title (see question in comments):

You can use parameters in the title (and other YAML metadata, such as author). For example:

rmd file

---
title: "Data for `r params$city`"
output: pdf_document
params:
  city: Dallas
---

Body of report

Separate R script to render the rmd file

Compile the rmd file for two cities:

for (i in c("New York", "Los Angeles")) {
  rmarkdown::render("test1.Rmd", 
                    params = list(city = i),
                    output_file=paste0(i, ".pdf"))
}

See the R Markdown Cookbook for additional info.

7
  • So you have to separate a script from the R markdown to loop trhough? Would it be more effective to create a function to do this?
    – akash87
    Jul 25, 2016 at 18:09
  • Yes to your first question (AFAIK). I don't know of a way to do it within the Rmarkdown document. As for your second question, if you're going to do this regularly, it might make sense to create a function to do it. Especially if you want to make it easy to customize or generalize the report.
    – eipi10
    Jul 25, 2016 at 18:12
  • I am still not clear as to why I need to specify My_City: Dallas in my Rmarkdown? Does it change when I use the for loop?
    – akash87
    Jul 26, 2016 at 13:53
  • Yes, because params = list(My_City = i) passes each City into the render function, changing that parameter each time through the loop. I suppose you could leave My_City: blank in the Rmarkdown document and see if it throws an error.
    – eipi10
    Jul 26, 2016 at 14:15
  • See update to my answer. You can use parameters in the report title and/or other YAML metadata.
    – eipi10
    Aug 11, 2021 at 17:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.