Lot of reports with a single click!

October 7, 2014
By

(This article was first published on MilanoR, and kindly contributed to R-bloggers)

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 on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Recent popular posts

Most visited articles of the week

  1. How to write the first for loop in R
  2. Installing R packages
  3. Using apply, sapply, lapply in R
  4. Tutorials for learning R
  5. How to Make a Histogram with Basic R
  6. How to perform a Logistic Regression in R
  7. Freedman's paradox
  8. Add P-values and Significance Levels to ggplots
  9. Deep Learning with R

Sponsors

RSS Jobs for R users

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)