[R] Using R to create pdf's from each file in a directory

Jeffrey Horner jeff.horner at vanderbilt.edu
Sat Apr 21 06:10:12 CEST 2007

gecko951 wrote:
> The Platform I am using R on is RHEL3.  I run a bash script that collects
> data into many CSV files and have been processing them one at a time on my
> local machine with an excel macro.  I would like to use R to take data
> points from each of the CSV files and create line graphs in PDF format
> because it will save me ALOT of time.  I am able to successfully do this
> when I call the file name directly...however my script bombs when I try to
> do multiple files.  I would like the created pdf's to have the same filename
> as the original csv files.  I have looked quite a bit and not found much
> help on "batch processing" an entire directory.  My current code is as
> follows:
> list <- dir("/tmp/data")
> for(x in list){
> d <- read.table(x, sep="\t", header=TRUE) # read data
> pdf("/tmp/graph/x.pdf")                              # file for graph
> plot(d$BlockSeqNum, d$MBs,                              # Blocks as x, MB/s
> as y     
>      type="l",                                     # plot lines, not points
>      xlab="Blocks",                                  # label x axis
>      ylab="MB/s",                                  # label y axis
>      main=x)          # add title
> dev.off()                                          # close file
> q()                                                # quit

Below will get you closer to what you want, assuming that your files end 
in .csv, which they should, especially if you'll be creating new files 
in the same directory with a different extension. You certainly don't 
want to re-run your R code and call read.table on a pdf. Another point 
is that your current working directory for R, returned by getwd(), is 
already '/tmp/data'. Otherwise read.table wouldn't work, and a more 
portable solution is to use a variable to hold the directory name:

workdir <- '/tmp/data'
for (x in dir(workdir,pattern='.csv$')){
   d <- read.table(paste(workdir,'/',x,sep=''), sep="\t", header=TRUE)
   plot(d$BlockSeqNum, d$MBs,



More information about the R-help mailing list