[R] Passing options as lists

Wilkinson, Mark Mark.Wilkinson at stjude.org
Thu Dec 5 20:10:03 CET 2002


Hi,

I apologize if this has previously been posted.  I've just subscribed to the
R-help digest.

I'm writing a plotting function that uses layout() to plot several different
plots on the same device.  This function uses plot(), image(), and a custom
function that uses text().  Each cell of the layout needs different par()
parameters, so what I'd like to do is pass them as lists:

my.plot.func <- function(data1, data2, data3,
                         data1.options=list(),
                         data2.options=list(),
                         data3.options=list()) {

  ## call layout to divide the device

  ## plot data1 with par options in data1.options
  ## plot data2 with par options in data2.options
  ## plot data3 with par options in data3.options

}

my.plot.func(data1, data2, data3, data1.options=list(arg1=12, arg2=FALSE,
cex=.8, las=3), data2.options=list(cex=2, lwd=3))

'. . .' wouldn't seem to work.

How can the user set the options with provided values, while retaining the
default arg values?  This is what I'm trying, but doesn't seem to work in
all cases, especially for '. . .':

args.full <- function(func.name, opt.list) {
  form <-formals(func.name)
  ddd <- c()
  
  rtn <- sapply(union(names(form), names(opt.list)), function(arg) {
    if (is.null(opt.list[[arg]])) {  # not user-defined
      form[[arg]]
    } else if (is.null(form[[arg]])) { # user-defined '...'
      ddd <<- c(ddd, opt.list[[arg]])
    } else {
      opt.list[[arg]]
    }
  })
  
  rtn[["..."]] <- ddd
  return(rtn)
}

opt1.data1 <- args.full("internal.plot.func", data1.options)

Then, within the function, setting each value to the corresponding name from
the return value of the above function with eval(), e.g.,
internal.plot.func(data1, arg1=eval(opt.data1$arg1),
arg2=eval(opt.data1$arg2), eval(opt.data1$...)).

One problem is with '. . .'; those par parameters don't get passed.

I'd also like this to work in general, not just for passing plotting
parameters.  Let me know if I haven't been clear.

Thanks,

Mark Wilkinson
Informatics Analyst
St. Jude Children's Research Hospital
Department of Pharmaceutical Sciences




More information about the R-help mailing list