[R] organizing plot drawing routines; creating complex expressions

From: Steven Lacey <slacey_at_umich.edu>
Date: Thu 08 Dec 2005 - 03:39:31 EST


My general goal is to find a coding strategy to efficiently store and retrieve drawing routines for different plots.  

This is my approach so far. In a single text file I store multiple drawing routines where each routine draws a different plot.  

A drawing routine may look like this:  

          panel.mean <- function(means,...){
                                 dots <- list(...)
                                 dots$x <- means$data$trial.bin
                                 dots$y <- means$data$Freq
                                 dots$typ <- "l"
                                 dots$lty <- 3
                                 dots$lwd <- 1
          arg <- list()
          arg$formula <- formula(Freq~trial.bin|subj)
          arg$group   <- quote(expected.hierarchyTo)
          arg$data <- atm.errors.intrusion.none[["frequency~hdt*prac*subj"]]
          arg$as.table <- TRUE
          arg$layout <- c(3,4)
          arg$col <- c("green","blue","red")
          arg$lwd <- 2
          arg$typ <- "b"
          arg$pch <- 17
          arg$lty <- 1
          arg$xlab <- "Trial Bin"
          arg$ylab <- "Frequency of Error Type"
          arg$main <- "ATM Experiment: Frequency of Intrusion-none Button
Presses During Error Trials\nAs A Function of Hierarchy Traversal Distance To Current Expected Button Press"
          arg$scales <- list(rot=c(90,90))
          arg$sub <- date()
          arg$means <-

list(data=gsummary(subset(arg$data,select=c("expected.hierarchyTo","trial.bi n","Freq")),form=~expected.hierarchyTo/trial.bin,mean))

          arg$par.settings <-

          arg$panel <-

          key <-

draw.key(list(background="white",border=TRUE,text=list(lab=levels(arg$data$e xpected.hierarchyTo)),lines=list(col=arg$col,pch=arg$pch,lty=arg$lty,lwd=arg $lwd)))  

vp<-viewport(x=0.9,y=grobHeight(key)+unit(0.07,"npc"),just=c("right","top"), width=grobWidth(key),height=grobHeight(key))


As you can see each routine is a sequence of commands required to draw a single plot. Each routine is separated in the text file by comments. If I wanted all these plots drawn I could source the file, but often I want only one. Each time I want to draw one of these I 1) open the text file, 2) scroll to the code for the desired plot, then copy and paste the code into the R console. Is there a more efficient way to store and retrieve these drawing routines?  

For example, I am exploring the following idea. Save the plot routines as expressions in a list object. The result is a list, l, with x components, where each component contains the routine to draw a plot as an expression. To draw a single plot all I need to do is type eval(l[[x]]).  

While this approach has succeeded, it has some drawbacks. One drawback comes about because I also want to be able to copy and paste directly from the routine as it is written into the R console. If I use the expression command to convert the routine to an expression, then each statement must be separated by commas. Using that syntax I cannot copy and paste command1 through command3 (see below) to the console because the commas will be included and cause a syntax error.  




Another option is to call parse(file="") and then run each command in sequence without commas. The problem here is that parse does not work on multi-line functions at the command line (see below). Why doesn't this work?  

> parse(file="")

expression(function(x) {

    x <- 9

> parse(file="")

Error in parse(file, n, text, prompt) : parse error

> parse(file="")

Error in parse(file, n, text, prompt) : parse error

Using parse also requires knowing how many statements will be processed ahead of time. If I go this route I would like some way to automate that as well.  

Assuming the parse problem described above cannot be fixed, there are two other possible options.  

  1. Rather than have multiple drawing routines in a single file, have each drawing routine in it's own file. In that case the syntax in the file is just as it would be at the command line, which is the way I want it. Furthermore, calling prase with a file now works--that is, it can handle multiple line functions with ease. The only real draw back to this is that having separate files for each and every plot may be overwhelming. I'd rather have a series of related plots in the same text file than the same folder. But, I could get over that.
  2. Convert the drawing routines to functions without arguments--not expressions. In this scheme each component of the list would contain a function with the drawing commands. This approach does work. The only reason I haven't implemented this and moved on is because a series of drawing commands seems intuitively more like a complex expression than a function. But, may be this is wrong. Comments?

Thanks for any help you can provide,
Steve Lacey  

> R.version

platform i386-pc-mingw32

arch     i386           
os       mingw32        
system   i386, mingw32  
major    2              
minor    1.1            
year     2005           
month    06             
day      20             

language R

        [[alternative HTML version deleted]]

R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Thu Dec 08 04:00:51 2005

This archive was generated by hypermail 2.1.8 : Thu 08 Dec 2005 - 09:30:47 EST