Lot of reports with a single click!

[This article was first published on MilanoR, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
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
  ds = read.csv("datapath/data.csv", header=T, sep=";", stringsAsFactors=F)

# load knitr library
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!

To leave a comment for the author, please follow the link and comment on their blog: MilanoR.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)