Skip to content

how to manipulate ... within a function

3 messages · Dan Kelley, Duncan Murdoch

#
Is there a way a function ('parent', say) can manipulate the ... argument,
and then pass the manipulated value into another function ('child', say)
that it calls?

What I'm trying to do is to use some plotting defaults within 'parent' that
are not part of parent's argument list, but to let the use over-ride these
defaults if they choose.  A sketch is below.

parent <- function(stuff, ...) {
  dots <- list(...)
  if (!"type" in names(list)) dots$type='p'
  plot(stuff, unlist(dots))  # or alist()? or what?
}

I know, I can just make "type" be a formal argument of 'parent', and I'm
happy to do that, but I'm still in the learning stages with R, and so I'm
trying to become more familiar with as much R syntax as I can, even if I end
up doing things differently later on.
#
On 18/12/2008 7:19 PM, Dan Kelley wrote:
You use the do.call() function to construct the call.  The first arg is 
what to call, the second is a list containing the args to pass.  Since 
you've got both stuff and dots to pass, you concatenate them into one 
big list:

do.call(plot, c(list(stuff), dots))

Duncan Murdoch
#
Thanks very much, Duncan.  In case it's of interest to others who encounter
this thread later, I am pasting below the code I wrote.  It is for the 'oce'
package for oceanographic data, which explains the reversal of the y axis,
forming a so-called 'profile', in which the top of the graph represents the
top of the water column.

        args <- list(x=x$data$temperature, y=x$data$pressure,
                     xlim=range(x$data$temperature),
                     log="",
                     ylim=rev(range(x$data$pressure)),
                     xlab=expression(paste("Temperature [", degree, "C ]")),
                     ylab="p [dbar]", ...)
        if (!("type" %in% names(list(...)))) args <- c(args, type="p")
        if (!("cex"  %in% names(list(...)))) args <- c(args, cex=0.3)
        do.call(plot, args)