1

I'm not having any success combining lapply, substitute, print, plot, and list in a function statement. Can someone direct me to a way to revise this code? I'd like to:

  1. automate plotting some but not all fields in a data frame vs. the first field in the df
  2. specify the fields to plot
  3. write the result to a file

The fields are numeric. Thanks.

brg<-as.data.frame(sqlFetch(channel,"Bearing"))               
is.data.frame(brg)                                            

varlist<-names(brg)[c(6,8,10,12)]                             
varlist                                                       
pdf(file="brg%d.pdf")                                         
figures<-lapply(varlist,function(x) {                         
print(plot(substitute(brg[c(1,i)], (list(i = as.name(x))))))
})  

update: Thanks for the comments. Here are two working versions of this code with axis lables (df read from SQL Server as written):

     #vers. 1
     brg<-as.data.frame(sqlFetch(channel,"Bearing"))
     is.data.frame(brg)
     dim(brg)
     head(brg)
     names(brg)<-tolower(names(brg))
     names(brg)
     varlist<-names(brg)[c(6,8,10,12)]
     varlist
     pdf(file="brgd.pdf")
     for (x in varlist) plot(brg[,1],brg[,x], xlab=names(brg[1]),ylab=as.name             (x))           
     dev.off()

     #vers. 2
     brg<-as.data.frame(sqlFetch(channel,"Bearing"))
     is.data.frame(brg) 
     dim(brg)
     head(brg)
     names(brg)<-tolower(names(brg))
     names(brg)
     varlist<-names(brg)[c(6,8,10,12)]
     varlist
     pdf(file="brg1.pdf")
     figures<-lapply(varlist,function(x) {

     (plot(brg[,1],brg[,x], ylab=as.name(x)))
     })
     dev.off()                                                 
user2009447
  • 93
  • 12
  • 1
    Please revise with a reproducible example. See [How to make a great reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – N8TRO Jan 25 '13 at 01:36
  • Please also describe what happens when you try the code above, and what you expect to happen. – Victor K. Jan 25 '13 at 01:37
  • I can certainly post an example if needed. The file is really just a table on SQL Server with many fields, some numeric, some character. – user2009447 Jan 25 '13 at 16:07
  • The code throws an error when the function statement is hit. The error is : Error in xy.coords......: invalid first argument. For what I expect to happen, see comment to answer below. Thanks for your advise. – user2009447 Jan 25 '13 at 16:12

1 Answers1

1

It is really not clear what you want to do , but I still have some comments about your code. I think here you try to complicate a simple task with , of plotting some variables ans save the plots in a file.

  1. No need to use substitute (it will give symbol), you can subset your data.frame with column names.
  2. No need to use lapply here since you don't save the result , just you want to repeat the plot.
  3. Why the the use of % in the name of file.
  4. You must close the device after plotting to unlock it.

    brg<- data.frame(a=rnorm(10),b= rnorm(10),   ## a reproducible data frame
                     c=rnorm(10))
    varlist<-names(brg)[c(1,2)] 
    pdf(file="brgd.pdf")                                                   
    for (x in varlist)   plot(brg[,1],brg[x])           ## the substitue would give brg[c(1,a)] !!
    dev.off()
    
agstudy
  • 119,832
  • 17
  • 199
  • 261
  • I appreciate the advise. The result of the above code block is a single pdf containing two plots, with each of the two variables plotted as y, separately. What I want is to produce individual pdfs for each plot, and each plot will be of the first variable in the data frame vs one of the specified subsequent variables in the data frame. So, first pdf ---> brg_field6.pdf, containing the plot of field 1 vs. field 6; second pdf ---> brg_field8.pdf, containing the plot of field 1 vs. field 8, etc. I'd like this process to run rapidly, as with apply functions, either with vectors or dfs. – user2009447 Jan 25 '13 at 15:42
  • Thanks. Incorporating your comments, the version using lapply in my original question now also works. – user2009447 Jan 29 '13 at 19:43