I am trying to automate reports in RMarkdown where I have two tables with different information in, but a site in common. I have written loops so that I can produce a summary report for each site, one for each table, but I can't get one of the tables to work. The first table always produces the same information, but the second is correct for each unique site.
I got so far using this post: R Knitr PDF: Is there a posssibility to automatically save PDF reports (generated from .Rmd) through a loop? but I can't get the two loops to print the same site in the same document:
R script:
set.seed(100)
Site<-c("A","B","C","D","E")
Species<-c("a","b","c","d","e")
Count<-rnorm(40,20,20)
df1<-data.frame(Site,Species,Count)
df1
Habitat<-c("v","w","x","y","z")
Size<-rnorm(40,20,20)
df2<-data.frame(Site,Habitat,Size)
df2
library(tidyverse)
library(rmarkdown)
Habitats<-df2 %>%
group_by(Site, Habitat) %>%
summarise(Total_Size=sum(Size))
Habitats
report1<-filter(Habitats,Site=='A')
report1
Species<-df1 %>%
group_by(Site,Species) %>%
summarise(Total_Count=sum(Count))
Species
#Habitats loop, but this bit doesn't work
for(Site in unique(Habitats$Site)){
subgroup1<-Habitats[Habitats$Site == Site,]
render("C:/Users/TVERC/Documents/GIS DataBase/P17-19 National Trust/template.rmd", output_file = paste0('report.', Site, '.html'))
}
#Species loop
for(Site in unique(Species$Site)){
subgroup2<-Species[Species$Site == Site,]
render("C:/Users/TVERC/Documents/GIS DataBase/P17-19 National Trust/template.rmd", output_file = paste0('report.', Site, '.html'))
}
template.rmd:
```{r, echo=FALSE}
#Report Analysis
Habs<-filter(subgroup1)
Habs
```
```{r, echo=FALSE}
Spp<-filter(subgroup2)
Spp
```
How do I do this please? And if I wanted to use 3, 4 or 5 tables all with data relating to the same sites, would your solution also work?