[Rd] "[", .local and S4 methods (was: "[" operator and indexing ambiguity)

From: Wolfgang Huber <huber_at_ebi.ac.uk>
Date: Sun 28 Jan 2007 - 14:40:31 GMT


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

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Mon Jan 29 01:46:24 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Mon 29 Jan 2007 - 11:31:33 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.