[R] Trouble with 3-dots
Peter Dalgaard BSA
p.dalgaard at biostat.ku.dk
Thu Oct 11 22:00:38 CEST 2001
David Brahm <a215020 at agate.fmr.com> writes:
> I wrote on 10/8/01 that I could not pass missing arguments from one function
> to another through "...":
>
> R> x <- matrix(1:12, 3,4)
> R> myfun <- function(x, ...) {if (missing(...)) x[]<-0 else x[...]<-0; x}
> R> myfun2 <- function(x, ...) myfun(x, ...)
> R> myfun2(x, ,1:2)
> Error in myfun(x, ...) : Argument is missing, with no default
>
> Here is one solution:
> R> myfun2 <- function(x, ...) {
> R> sc <- sys.call()
> R> sc[[1]] <- as.name("myfun")
> R> eval.parent(sc)
> R> }
>
> R> myfun2(x, ,1:2)
> [,1] [,2] [,3] [,4]
> [1,] 0 0 7 10
> [2,] 0 0 8 11
> [3,] 0 0 9 12
>
> Comments? Is this in any way inefficient or dangerous? Thanks.
If you use match.call rather than sys.call you get a construction type
that is used a couple of places in the R sources. Not dearly beloved
by its authors, but used nevertheless. (See lm, for instance)
Generally, combining missing and ... is asking for trouble and any
solution is going to be ugly in some way.
There might be a cleaner way involving looking at
match.call(expand.dots=FALSE)$...
but I don't quite see it. Be careful, there are lots of little
surprises in this stuff.
Time might be better spent figuring out why
> "[<-"(x,value=0)
Error: SubAssignArgs: invalid number of arguments
whereas x[]<-0 works fine. (So does "[<-"(x,,value=0), which could be
part of the explanation).
--
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
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list