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

Duncan Murdoch murdoch at stats.uwo.ca
Sat Apr 21 04:09:18 CEST 2007


On 4/20/2007 9:40 PM, 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
> 
> ERROR: Error in plot.window(xlim, ylim, log, asp, ...) :
>         need finite 'xlim' values
> In addition: Warning messages:
> 1: no non-missing arguments to min; returning Inf
> 2: no non-missing arguments to max; returning -Inf
> 3: no non-missing arguments to min; returning Inf
> 4: no non-missing arguments to max; returning -Inf

What you're doing appears to be a reasonable approach; all you need are 
some checks that the data is reasonable before calling plot and possibly 
bombing.

I'd suggest adding a "print(x); print(str(d))" statements after you read 
d; then you'll be able to see when it is that sometimes d doesn't 
contain plotable data.

One other problem you'll find:  you're writing all plots to "x.pdf".  I 
think you really want to construct that name differently for each x. 
I'd suggest using paste() and basename() (and perhaps some regular 
expressions and gsub()) to construct the output filename.

Duncan Murdoch



More information about the R-help mailing list