3

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?

mx0
  • 6,445
  • 12
  • 49
  • 54
Dan Carpenter
  • 101
  • 1
  • 5

1 Answers1

0

In your template.rmd you are calling subgroup2

{r, echo=FALSE} Spp<-filter(subgroup2) Spp but it is not created untill you enter the second loop.

#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'))
}

so when you run

#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'))
}

It does not exist yet. Perhaps something along those lines would help ?

for(Site in unique(c(Species$Site,Habitats$Site)){
  subgroup1<-Habitats[Habitats$Site == 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'))
}
Cedric
  • 2,412
  • 17
  • 31
  • Thanks @Cedric, but this didn't work for me as I got an error saying that '$ operator is invalid for atomic vectors'. I think this is because you can't have attributes for atomic vectors. Any suggestions please? – Dan Carpenter Nov 17 '17 at 12:28
  • @Dan. I'm sorry, you are right, it's a typo (change Habitat with Habitats) I've corrected my script and tested it, it works.... – Cedric Nov 17 '17 at 16:16