1
votes

I wrote an R script to read data from csv file and print these data into different Word Doc (docx) files by using the following code. This is working as expected without any glitch. But what I don't know to achieve is to use 'knitr' or 'sweave' to output pdf files instead. Can some one direct me how to make the script to output pdf files instead of docx?

library(ggplot2)
library(reshape)
lff <- list.files(path = "./data/", pattern = paste('*.csv*',sep=""), 
 full.names = TRUE, recursive = TRUE, include.dirs = TRUE) 
for (i in 1:length(lff)) {
....

  writeDoc( doc, filename )
}
2
you should provide a replicable example instead of putting ... in your loop. That would make it easier to help you.Giacomo

2 Answers

0
votes

You can use gridExtra package:

library(gridExtra)
library(stringr)

lff <- list.files(path = "./data/", pattern = paste('*.csv*',sep=""), 
                  full.names = TRUE, recursive = TRUE, include.dirs = TRUE) 

for (i in lff) {
  dt <- read.csv(i, header=TRUE)
  pdf(paste0(str_replace(i, '.csv', ''),'.pdf'), height=11, width=8.5)
  grid.table(dt)
  dev.off()
}
0
votes

Here is a solution for knitr with Sweave, since you need to loop over something,say i,base on which your Script.Rnw would produce different results, other wise loop will be useless. See code below :

## elements in `names` could also be parameters in your `Script.Rnw`
names<-c('first','second','third')
for (i in names) {
  output.name<-paste0(i,'.tex')
  knit2pdf('Script.Rnw',output = output.name)
} 

at the end you will have 'first.pdf','second.pdf','third.pdf' For shiva's case, elements in names could be the names of her csv files. Base on the comment of @giac_man, you could write how to read in your csv file in a .Rnw file and since they are csv I guess you may could use xtable in that .Rnw file too to print your csv file and LateX is needed too to get those PDFs, I'm currently using MikTex.