How to create a different report for each subset of a data frame with R markdown? -


i have dataset 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 build report using r markdown; however, each city need build report. reason 1 city cannot see report city. how build report , save pdf of each city?

each report need median score, mean count, , mean returns. know using dplyr use

finaldat <- dat %>%             group_by(city) %>%             summarise(score = median(score),                       count = mean(count)  ,                       return= mean(returns)) 

but frustration comes producing report each city. also, subset of data, not full data. is, report extensive , report of results, systematic, not different each city.

it looks parameterized report might need. see link details, basic idea set parameter in yaml of rmarkdown report , use parameter within report customize (for example, filtering data city in case). in separate r script, render report multiple times, once each value of city, pass parameter render function. here's basic example:

in rmarkdown report declare parameter in yaml. listed value, dallas in case, default value if no other value input when render report:

--- title: document output: pdf_document params:    my_city: dallas --- 

then, in same rmarkdown document have entire report--whatever calculations depend on city, plus boilerplate that's same city. access parameter params$my_city. code below filter data frame current value of my_city parameter:

```{r} dat %>%             filter(city==params$my_city) %>%     summarise(score = median(score),               count = mean(count)  ,               return= mean(returns)) ``` 

then, in separate r script, following produce separate report each city (where i've assumed rmarkdown file above called myreport.rmd):

for (i in unique(dat$city)) {     rmarkdown::render("myreport.rmd",                        params = list(my_city = i),                       output_file=paste0(i, ".pdf")) } 

in code above, i've assumed dat data frame in global environment of separate r script renders myreport.rmd. however, provide vector of city names instead of getting names unique(dat$city).


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -