Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Suppose you want to create a huge number of pdf files through RMarkdown and pandoc, each of them including a statistical analysis on a part of your data, for example on each row of your data frame.

You need to write a .R file with cycles from 1 to the number of rows of your data set the instructions contained in a .Rmd file.

Suppose that:

1. your data set name is data.csv and its path is datapath/data.csv’’
2. your .Rmd file name is report.Rmd and its path is basepath/report.Rmd’’

An R code to produce $n$ pdf, where $n$ is the number of rows of your data set could be something like this:

# load data

require(knitr)

# path
basepath = insert_your_basepath
setwd(basepath)

# pdf cycle
for (i in 1:nrow(ds)) {

# input Rmd file name and output pdf file names
SRC = "report.Rmd"
OUT = paste0("output_report_”, i")

# useful strings for pandoc
PANDOC_TEX_OPTIONS = " -s -N "
MD2PDF = paste0("pandoc", PANDOC_TEX_OPTIONS, " -o ", OUT, ".pdf ", OUT, ".md")

# knit and pandoc
knit(SRC, output = paste0(OUT, ".md"))
system(MD2PDF)
}

# let us delete .md files
system("rm *.md")

In next article I will explain to you how send by mail these $n$ PDF files to $n$ different recipients simply exploiting the power of R!