[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.
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:
- your data set name is
data.csv
and its path is datapath/data.csv’’ - your .Rmd file name is
report.Rmd
and its path is basepath/report.Rmd’’
An R code to produce
# 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