Dear R users, I want to retrieve "..." argument values within a function. Here is a small exmaple: myfunc <- function(x, ...) { if (hasArg(ylim)) a <- ylim plot(x, ...) } x <- rnorm(100) myfunc(x, ylim=c(-0.5, 0.5)) Error in myfunc(x, ylim = c(-0.5, 0.5)) : Object "ylim" not found>I need to retrieve values of "ylim" (if it is defined when function is called) for later use in the function. Can anybody give me some hint? Thanks a lot. Huan This message and any attachments (the "message") is\ intende...{{dropped}}
On Tue, 16 Sep 2003 huan.huang at bnpparibas.com wrote:> > Dear R users, > > I want to retrieve "..." argument values within a function. Here is a small > exmaple: > > myfunc <- function(x, ...) > { > if (hasArg(ylim)) a <- ylim > plot(x, ...) > } >One solution is dots<-substitute(list(...)) a<-dots$ylim which sets a to NULL if there is no ylim argument and to the ylim argument if it exists -thomas
For most purposes a more useful technique is to write the function with a default NULL argument myfunc <- function(x, ylim=NULL) so that it can be called as myfunc(x) or myfunc(x,y). Inside the function you test for !is.null(ylim) and take appropriate action. Alternatively, and maybe more commonly, you give ylim a sensible default so the caller has to be explicit about setting ylim to NULL if required. As it happens, in your specific case you can write as you did except for the pseudo-line(hasArg(ylim)), because all arguments will get passed to plot, which will itself recognise and test for an argument called ylim, within its own "...". HTH> -----Original Message----- > From: huan.huang at bnpparibas.com [mailto:huan.huang at bnpparibas.com] > Sent: 16 September 2003 15:14 > To: r-help at stat.math.ethz.ch > Subject: [R] Retrieve ... argument values > > > Security Warning: > If you are not sure an attachment is safe to open please contact > Andy on x234. There are 0 attachments with this message. > ________________________________________________________________ > > > Dear R users, > > I want to retrieve "..." argument values within a function. Here is a > small > exmaple: > > myfunc <- function(x, ...) > { > if (hasArg(ylim)) a <- ylim > plot(x, ...) > } > > x <- rnorm(100) > myfunc(x, ylim=c(-0.5, 0.5)) > Error in myfunc(x, ylim = c(-0.5, 0.5)) : Object "ylim" not found > > > > I need to retrieve values of "ylim" (if it is defined when function is > called) for later use in the function. Can anybody give me some hint? > > Thanks a lot. > > Huan > > > > > This message and any attachments (the "message") is\ > intende...{{dropped}} > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >Simon Fear Senior Statistician Syne qua non Ltd Tel: +44 (0) 1379 644449 Fax: +44 (0) 1379 644445 email: Simon.Fear at synequanon.com web: http://www.synequanon.com Number of attachments included with this message: 0 This message (and any associated files) is confidential and\...{{dropped}}
huan.huang at bnpparibas.com writes:> Dear R users, > > I want to retrieve "..." argument values within a function. Here is a small > exmaple: > > myfunc <- function(x, ...) > { > if (hasArg(ylim)) a <- ylim > plot(x, ...) > } > > x <- rnorm(100) > myfunc(x, ylim=c(-0.5, 0.5)) > Error in myfunc(x, ylim = c(-0.5, 0.5)) : Object "ylim" not found > > > > I need to retrieve values of "ylim" (if it is defined when function > is called) for later use in the function. Can anybody give me some > hint?Yes, several: "ylim" %in% names(match.call(expand.dots=FALSE)$...) or "ylim" %in% names(list(...) (Use the former if it is somehow important not to evaluate the arguments). Or even a <- list(...)$ylim and then check for is.null(a). -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Huan - Look at the function code for order(). To show the function definition, type just order at the command line (no quotes, no parentheses). This example is what I found most useful when I had a similar question. The green book is also useful. - tom blackwell - u michigan medical school - ann arbor - On Tue, 16 Sep 2003 huan.huang at bnpparibas.com wrote:> Dear R users, > > I want to retrieve "..." argument values within a function. > Here is a small exmaple: > > myfunc <- function(x, ...) > { > if (hasArg(ylim)) a <- ylim > plot(x, ...) > } > > I need to retrieve values of "ylim" (if it is defined when > function is called) for later use in the function. Can anybody > give me some hint? Thanks a lot. > Huan
Try: myfunc <- function(x, ...) { if (hasArg(ylim)) a <- ...$ylim plot(x, ...) } HTH, Andy> -----Original Message----- > From: huan.huang at bnpparibas.com [mailto:huan.huang at bnpparibas.com] > Sent: Tuesday, September 16, 2003 10:14 AM > To: r-help at stat.math.ethz.ch > Subject: [R] Retrieve ... argument values > > > > Dear R users, > > I want to retrieve "..." argument values within a function. > Here is a small > exmaple: > > myfunc <- function(x, ...) > { > if (hasArg(ylim)) a <- ylim > plot(x, ...) > } > > x <- rnorm(100) > myfunc(x, ylim=c(-0.5, 0.5)) > Error in myfunc(x, ylim = c(-0.5, 0.5)) : Object "ylim" not found > > > > I need to retrieve values of "ylim" (if it is defined when > function is called) for later use in the function. Can > anybody give me some hint? > > Thanks a lot. > > Huan > > > > > This message and any attachments (the "message") is\ > intende...{{dropped}} > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo> /r-help >------------------------------------------------------------------------------ Notice: This e-mail message, together with any attachments,...{{dropped}}
Yes, and I was wrong to say ylim=NULL was "more useful", I should have said "much easier to understand and much easier to read, debug and maintain". Of course for certain applications it IS worth getting to grips with "...", and other people's posts have been extremely useful in that regard.> -----Original Message----- > From: Ben Bolker [mailto:bolker at zoo.ufl.edu] > Sent: 16 September 2003 16:18 > To: Simon Fear > Cc: huan.huang at bnpparibas.com; R help list > Subject: RE: [R] Retrieve ... argument values > > > Security Warning: > If you are not sure an attachment is safe to open please contact > Andy on x234. There are 0 attachments with this message. > ________________________________________________________________ > > > Yes, although this becomes tedious if (e.g.) you have a > function that > calls two different functions, each of which has many arguments (e.g. > plot() and barplot(); then you have to set up a whole lot of > arguments > that default to NULL and, more annoyingly, you have to > document them all > in any .Rd file you create -- rather than just having a ... argument > which > you can say should contain arguments for either of the > subfunctions (as > long as the arguments don't overlap, of course) > > Ben > >Simon Fear Senior Statistician Syne qua non Ltd Tel: +44 (0) 1379 644449 Fax: +44 (0) 1379 644445 email: Simon.Fear at synequanon.com web: http://www.synequanon.com Number of attachments included with this message: 0 This message (and any associated files) is confidential and\...{{dropped}}
Ben Bolker <bolker at zoo.ufl.edu> wrote: Yes, although this becomes tedious if (e.g.) you have a function that calls two different functions, each of which has many arguments (e.g. plot() and barplot(); then you have to set up a whole lot of arguments that default to NULL and, more annoyingly, you have to document them all in any .Rd file you create -- rather than just having a ... argument which you can say should contain arguments for either of the subfunctions (as long as the arguments don't overlap, of course) Could I suggest a very simple alternative, which extends gracefully to any number of arguments, and doesn't require you to know anything much about ... except that you can pass it to another function?> f <- function(...) {+ has.ylim <- function(..., ylim) return(!missing(ylim)) + + if (has.ylim(...)) "yes" else "no" + }> f(xlim=1)[1] "no"> f(ylim=2)[1] "yes" For each parameter XXX that you want to test the presence of, write has.XXX <- function(..., XXX) return(!missing(XXX)) and then use has.XXX(...) in the body of the main function.