[R] How to specify function arguments that are used "in different places"

Gabor Grothendieck ggrothendieck at gmail.com
Mon May 1 15:36:58 CEST 2006


You could have a list of args for each one like this:

# test data
x <- list(data = c(1,3,5), points = c(2,4))

myfunc <- function(x, plot.args = NULL, points.args = NULL) {
	do.call("plot", c(list(x$data), plot.args))
	do.call("points", c(list(x$points), points.args))
}
myfunc(x, plot.args = list(col = "red"), points.args = list(col = "blue"))


On 5/1/06, Gregor Gorjanc <gregor.gorjanc at gmail.com> wrote:
> Hello!
>
> Subject is not very clear, but I hope my question will be;) I wrote a
> function, which produces a plot and I have problems with arguments. For
> the sake of example let us consider that my function looks like this
>
> myfunc <- function(x, points=FALSE, lines=FALSE, ...)
> {
>  ## x is an object that is being plotted
>  plot(x$plotData, ...)
>  ## one can also add some data on graph via points
>  points(x$pointsData, ...)
>  ## one can also add some data on graph via lines
>  lines(x$linesData, ...)
> }
>
> My problem is in "..." argument. plot(), points() and lines() have so
> many possible arguments, which is very nice, but how can I deal with
> them in my scenario. For example, I might want to specify red color for
> plot, blue for points and green for lines. Is it possible to handle such
> a mixture, without specifiying zillion of arguments such as plotCol,
> pointsCol, linesCol etc.? Perhaps something like ~ "points$..."?
>
> Thanks!
>
> --
> Lep pozdrav / With regards,
>    Gregor Gorjanc
>
> ----------------------------------------------------------------------
> University of Ljubljana     PhD student
> Biotechnical Faculty
> Zootechnical Department     URI: http://www.bfro.uni-lj.si/MR/ggorjan
> Groblje 3                   mail: gregor.gorjanc <at> bfro.uni-lj.si
>
> SI-1230 Domzale             tel: +386 (0)1 72 17 861
> Slovenia, Europe            fax: +386 (0)1 72 17 888
>
> ----------------------------------------------------------------------
> "One must learn by doing the thing; for though you think you know it,
>  you have no certainty until you try." Sophocles ~ 450 B.C.
>
> ______________________________________________
> R-help at 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
>




More information about the R-help mailing list