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

Robert A LaBudde ral at lcfltd.com
Sat Apr 21 05:30:00 CEST 2007


At 11:06 PM 4/20/2007, I wrote:
>At 09:40 PM 4/20/2007, gecko951 wrote:
> ><snip>
> >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
> ><snip>
>
>I'm a tyro at R, but it's obvious here that the line
>
>pdf("/tmp/graph/x.pdf")
>
>has the intended file name string 'x' embedded with the literal
>string enclosed in quotation marks. Obviously he needs "/tmp/graph/"
>string concatenated with x and then ".pdf".
>
>I would suggest a fix, but I am unable to find in the documentation
>how to concatenate two strings into a single string.
>
>So I will amplify gecko951's question to include "How you you
>concatenate two strings in R?". I.e.,
>
>x<-"abc"
>y<-"def"
>
>What operator or function returns "abcdef" as a result?
><snip>

I found that paste() can be used for string concatenation, so I can 
now suggest a fix to gecko951's problem. Replace

pdf("/tmp/graph/x.pdf")

with

pdf(paste(paste('/tmp/graph/',x,sep=''),'.pdf',sep=''))

and the program should now work for the different file names.

================================================================
Robert A. LaBudde, PhD, PAS, Dpl. ACAFS  e-mail: ral at lcfltd.com
Least Cost Formulations, Ltd.            URL: http://lcfltd.com/
824 Timberlake Drive                     Tel: 757-467-0954
Virginia Beach, VA 23464-3239            Fax: 757-467-2947

"Vere scire est per causas scire"



More information about the R-help mailing list