Some years ago I had to do a lot of reports for different breeders. They were reports based on the results of a research and it was the same report with data for each separated herd. Logically, I could have done it in Word, one by one, but they were around 50 herds. So I looked for a way to make them all in one go.
At that time I was using R, so I did the job in it, using the package rmarkdown.
Let’s start by generating data for the example (all in R):
id=1:100
client=rep(LETTERS[1:10],10)
y=rnorm(100)
ex_data=data.frame(id,client,y)
write.table(ex_data,"ex_data.dat",quote=F,row.names = F,sep=" ")
As you can see in the code above, we generate identifications (column 1), clients (column 2) and the observations (column 3).
We will use an .Rmd file as model to render the reports. In this case the output format will be a pdf. I saved the file as model.Rmd:
---
title: 'Example of Multiple reports'
author: www.nanopuntouy.info
output: pdf_document
---
```{r, echo=FALSE}
client=unique(tmp$client)
# we need knitr package to use the function kable() later
library(knitr)
```
# This is an example of a report to the client "`r client`"
## Just an introduction:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris finibus erat et ultrices tincidunt. Cras vel dignissim dui. Maecenas eget ligula magna. Nulla facilisi. Nunc orci ante, interdum eget turpis a, eleifend semper libero. Maecenas a neque ligula. Sed facilisis pretium rutrum.
## Table of the data
```{r, echo=FALSE}
kable(tmp[,c(1,3)],row.names = F)
```
## An histogram
```{r,echo=FALSE}
hist(tmp$y, main="The distribution of your data", xlab = "Your data")
```
## Mean max and min
The average in your case was `r mean(tmp$y)`.
The minimum value was `r min(tmp$y)`.
Maximum value was `r max(tmp$y)`.
If you are not familiar with rmarkdown you can find information about the package here. But in short, with rmarkdown we can write markdown files with embedded code that will be processed during the rendering. The code is include by chunks and you will recognize them because, for example, for R language they start by “```
{r}” and finish with “```
“. In the file we work with a data.frame called tmp that we will generate in the next script (the one to generate the reports).
The script to render the reports is:
# We call the rmarkdown package
library(rmarkdown)
# Read the data file
ex_data=read.table("ex_data.dat",header = T)
# A loop to generate the reports
for( i in unique(ex_data$client)){
# Filter the data for each client
tmp=ex_data[ex_data$client==i,]
# A name for the output file
output <- paste("report_client_",i,".pdf",sep="")
# We use the render() function of rmarkdown package to render the reports
render("model.Rmd",output_file = output, params=list(tmp=tmp))
}
Using render() function of the package rmarkdown we generate the reports. In this case the arguments that I used were: “model.Rmd” (the name of the model I want to use), output_file=output (the name of the output file, will be one for each client) and params (a parameter that say to render() which data it have to use; in this case tmp).
So, render() will read model.Rmd and will process it with the data for each client in each round of the loop.
To test the script you have to put all the three files (ex_data.dat, model.Rmd, and the script to render) in the same directory.
Also (1), you have to install the packages knitr and rmarkdown. You could have some troubles with java, but there are information in google to fix the problems (it will depend on the kind of problem).
Also (2), you will need a latex installation in your computer. See how to install it in the different operating systems (you could need additional latex packages, mainly in windows).
You can download an example of a report using the follow link