Re: [Rd] Extending [ method to S4 class and drop argument (PR#9211)

From: <mtmorgan_at_fhcrc.org>
Date: Fri 08 Sep 2006 - 17:21:28 GMT


Or more in the spirit of method dispatch, and to get some insight into why this design decision is not a bug:

> setClass("test",representation(x="character"))
[1] "test"
>
> setMethod("[",

+           signature(x="test", drop="missing"),
+           function(x, i, j, ..., drop) {
+               cat("here\n")
+               callNextMethod(x, i, j, ..., drop=TRUE)
+       })

[1] "["
>
> setMethod("[","test",function(x, i, j, ..., drop) {
+     cat("there\n")
+     print(drop)
+ })

[1] "["
>
> a = new("test",x="fred")
> a[1]

here
there
[1] TRUE 'drop' is 'missing', and 'missing' satisfies the implicit 'ANY' condition in the signature of the original method. With this insight, the design decision to 'ignore' the default argument seems appropriate to me -- developers implementing the generic might well want to dispatch on it, since after all it is in the generic signature. This could lead to an explosion of methods if many arguments have 'default' values, but such an explosion probably points to a nice distinction between arguments-for-dispatch and arguments-for-evaluation.

To tell the truth, I hadn't really understood why the error originally reported occurred, until writing the method with drop="missing" just now.

Martin

"Gabor Grothendieck" <ggrothendieck@gmail.com> writes:

> Try this:

>
>> setClass("test",representation(x="character"))

> [1] "test"
>> setMethod("[","test",function(x,i,j,...,drop) {

> + print(i)
> + if (missing(drop)) drop <- TRUE
> + print(drop)
> + })
> [1] "["
>> a = new("test",x="fred")
>> a[1]

> [1] 1
> [1] TRUE
>
>

> On 9/8/06, jverzani@gmail.com <jverzani@gmail.com> wrote:
>> Full_Name: John Verzani
>> Version: 2.4.0 alpha (2006-09-05 r39134)
>> OS: linux, gentoo 2.6.17
>> Submission from: (NULL) (163.238.43.26)
>>
>>
>> When extending the "[" method to a new S4 class, the default value for the drop
>> argument is not being found. Here is a small example:
>>
>> setClass("test",representation(x="character"))
>> setMethod("[","test",function(x,i,j,...,drop=TRUE) {print(i);print(drop)})
>> a = new("test",x="fred")
>> a[1]
>>
>> resulting in:
>>
>> [1] 1
>> Error in print(drop) : argument "drop" is missing, with no default
>>
>> I'm expecting TRUE for the value of drop. That's correct isn't?
>>
>> ______________________________________________
>> R-devel@r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>

> ______________________________________________
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
-- 
Martin T. Morgan
Bioconductor / Computational Biology
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024
Seattle, WA 98109

http://bioconductor.org

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Sat Sep 09 03:25:20 2006

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 Fri 08 Sep 2006 - 18:30:04 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.