Dear list, I wonder how to write methods for the function 'as' in the sense that I can call 'as(object, Class, strict=TRUE, ext)' and let method dispatch figure out the correct method. AFAIU, there is a difference between, e.g. 'as.data.frame' and the methods of 'as()' as stated above since the former depends on arg 'x' instead of 'object', 'Class' etc.? > methods("as") > as.data.frame I have to admit that I'm not really familiar with the S3 style of defining methods as I have been coding in S4 a lot, but my first attempt was to write something like this: as.myClass <- function(x, ...){ if(is(x, "data.frame"){ x <- as.list(x) } if(is(x, "character"){ x <- as.list(x) } ... out <- getRefClass("myClass")$new(X=x) return(out) } But that way I'd have to explicitly call 'as.myClass(x)' whereas I'd simply like to type 'as(x, "myClass")'. Also, how is it possible to have method dispatch recognize two signature arguments in S3? I.e., how can I define something like 'as.data.frame.character' in order to have explicit "sub" methods for all the data types of 'x' so I wouldn't have to process them all in the definition of 'as.myClass' as I did above? Thanks for your help, Janko -- ------------------------------------------------------------------------ *Janko Thyson* janko.thyson@googlemail.com <mailto:janko.thyson@googlemail.com> Jesuitenstraße 3 D-85049 Ingolstadt Mobile: +49 (0)176 83294257 This e-mail and any attachment is for authorized use by the intended recipient(s) only. It may contain proprietary material, confidential information and/or be subject to legal privilege. It should not be copied, disclosed to, retained or used by any other party. If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender. [[alternative HTML version deleted]]
Somehow I don't see my own postings in the list, so sorry for replying to my own message and not the one that went out to the list. I got a little further and I think I found exactly the thing that is bothering me: how to get "extended" method dispatch going in 'setAs()': setRefClass("A", fields=list(X="numeric")) setRefClass("B", contains="A", fields=list(Y="character")) setAs(from="numeric", to="A", def=function(from,to){ out <- getRefClass(to)$new(X=from) return(out) } ) a <- as(1:5, "A") a$X b <- as(1:5, "B") My problem is the last statement (b <- as(1:5, "B") which fails. I want to get around having to write new 'setAs' methods for all classes extending class 'A'. If 'B' inherits from 'A', shouldn't it then be possible to tell 'setAs' to look for the next suitable method, i.e. the method defined for 'A'? I tried 'NextMethod()' inside the body of 'setAs' but that didn't work out. Thanks a lot, Janko On 06.06.2011 17:15, Janko Thyson wrote:> Dear list, > > I wonder how to write methods for the function 'as' in the sense that > I can call 'as(object, Class, strict=TRUE, ext)' and let method > dispatch figure out the correct method. > > AFAIU, there is a difference between, e.g. 'as.data.frame' and the > methods of 'as()' as stated above since the former depends on arg 'x' > instead of 'object', 'Class' etc.? > > > methods("as") > > as.data.frame > > I have to admit that I'm not really familiar with the S3 style of > defining methods as I have been coding in S4 a lot, but my first > attempt was to write something like this: > > as.myClass <- function(x, ...){ > if(is(x, "data.frame"){ > x <- as.list(x) > } > if(is(x, "character"){ > x <- as.list(x) > } > ... > out <- getRefClass("myClass")$new(X=x) > return(out) > } > > But that way I'd have to explicitly call 'as.myClass(x)' whereas I'd > simply like to type 'as(x, "myClass")'. > Also, how is it possible to have method dispatch recognize two > signature arguments in S3? I.e., how can I define something like > 'as.data.frame.character' in order to have explicit "sub" methods for > all the data types of 'x' so I wouldn't have to process them all in > the definition of 'as.myClass' as I did above? > > Thanks for your help, > Janko[[alternative HTML version deleted]]
Maybe Matching Threads
- Ref Classes: bug with using '.self' within initialize methods?
- Reference classes: accessor functions via 'getRefClass(...)$accessors(...)'
- Reference Classes: removing methods -> implications for objects/instances of that class
- WG: Reference classes: error with missing arguments in method calls
- Query super- and subclasses of a class: is there a better way than to use 'completeClassDefinition()'