Thanks a bunch to those who replied to my convoluted question.
Wiener, Matthew [matthew_wiener at merck.com] says to use
do.call("plot",
args.list).
This is what I chose to do.
In addtion to do.call(), Mark.Bravington at csiro.au suggested incrementally
building the call:
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
call1_ quote( plot( data1)) # the basic call, as an expression
call1_ as.call( c( as.list( call1), data1.options)) # tack on the extra
args
eval( call1) # evaluate it
<< rest of code>>
}
Mark Wilkinson
Informatics Analyst
St. Jude Children's Research Hospital
Department of Pharmaceutical Sciences
-----Original Message-----
From: Wilkinson, Mark
Sent: Thursday, December 05, 2002 1:08 PM
To: 'r-help at stat.math.ethz.ch'
Subject: Passing options as lists
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