[Rd] "[", .local and S4 methods (was: "[" operator and indexing ambiguity)
Wolfgang Huber
huber at ebi.ac.uk
Sun Jan 28 15:40:31 CET 2007
Dear Tony,
thanks for the tip with "nargs", when suitably applied, this answers the
problem.
The behaviour of "nargs" in S4 methods has some subtleties compared to
that in normal functions, as shown in the example below. I admit that
this is what had earlier created some confusion about the semantics of
"nargs". From the perspective of "nargs" and its man page, this all
seems fine, but its interaction with the S4 methods implementation was
surprising to me and I'd be interested whether people think that this is
the desired behaviour.
### ------------------- example code ---------------------------
setClass("foo", representation(x="array"))
a = new("foo")
myfun = function (x, i, j, k, ..., drop=TRUE) {
cat(paste(match.call()), "\nnargs()=", nargs(),
missing(i), missing(j), missing(k), missing(drop), "\n\n")
return(invisible(NULL))
}
setMethod ("[", signature(x="foo", i="numeric", j="missing"), myfun)
a[7]
a[7,]
myfun(a, 7)
myfun(a, 7, )
showMethods("[", classes="foo", inc=TRUE)
### --------------------- example output -----------------------
.local x i j drop
nargs()= 4 FALSE TRUE TRUE FALSE
.local x i j drop
nargs()= 4 FALSE TRUE TRUE FALSE
## here it seems impossible to distinguish a[7] from a[7,]
## also note that missing(drop) is FALSE
myfun a 7
nargs()= 2 FALSE TRUE TRUE TRUE
myfun a 7
nargs()= 3 FALSE TRUE TRUE TRUE
Function: [ (package base)
x="foo", i="numeric", j="missing"
function (x, i, j, ..., drop = TRUE)
{
.local <- function (x, i, j, k, ..., drop = TRUE)
{
cat(paste(match.call()), "\nnargs()=", nargs(), missing(i),
missing(j), missing(k), missing(drop), "\n\n")
return(invisible(NULL))
}
.local(x, i, j, ..., drop = drop)
}
### ----------- example code 2 (different signature: no k) ---
myfun = function (x, i, j, ..., drop=TRUE) {
cat(paste(match.call()), "\nnargs()=", nargs(),
missing(i), missing(j), missing(drop), "\n\n")
return(invisible(NULL))
}
setMethod ("[", signature(x="foo", i="numeric", j="missing"), myfun)
a[7]
a[7,]
myfun(a, 7)
myfun(a, 7, )
showMethods("[", classes="foo", inc=TRUE)
### --------------------- example output -----------------------
[ a 7
nargs()= 2 FALSE TRUE TRUE
[ a 7
nargs()= 3 FALSE TRUE TRUE
myfun a 7
nargs()= 2 FALSE TRUE TRUE
myfun a 7
nargs()= 3 FALSE TRUE TRUE
Function: [ (package base)
x="foo", i="numeric", j="missing"
function (x, i, j, ..., drop = TRUE)
{
cat(paste(match.call()), "\nnargs()=", nargs(), missing(i),
missing(j), missing(drop), "\n\n")
return(invisible(NULL))
}
########################################
> sessionInfo()
R version 2.5.0 Under development (unstable) (2007-01-28 r40602)
i686-pc-linux-gnu
locale:
LC_CTYPE=en_GB.UTF-8;LC_NUMERIC=C;LC_TIME=en_GB.UTF-8;LC_COLLATE=en_GB.UTF-8;LC_MONETARY=en_GB.UTF-8;LC_MESSAGES=en_GB.UTF-8;LC_PAPER=en_GB.UTF-8;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_GB.UTF-8;LC_IDENTIFICATION=C
attached base packages:
[1] "stats" "graphics" "grDevices" "utils" "datasets" "methods"
[7] "base"
Best wishes
--
------------------------------------------------------------------
Wolfgang Huber EBI/EMBL Cambridge UK http://www.ebi.ac.uk/huber
More information about the R-devel
mailing list