Re: [R] How can I write methods for 'as()'?

2011-06-15 Thread Jamie Olson
Since nobody else has respond, I thought I'd take a stab.  Maybe if I'm
wrong enough somebody will correct me, but my understanding is that that
kind of situation, ie the pain of getting the correct method called when
there is a dependency on the type of more than one argument is part of the
motivation behind the S4 classes.  So maybe it really just is that kludgy
for s3 classes.

Jamie Olson
School of Computer Science
Carnegie Mellon University
5000 Forbes Ave.
Pittsburgh, PA 15213
jfol...@cs.cmu.edu


On Mon, Jun 6, 2011 at 12:19 PM, Janko Thyson 
janko.thyson.rst...@googlemail.com wrote:

 Okay, I found something that is working, but it looks and feels pretty
 awkward as the method def and method lookup takes place in one function ;-)

 setRefClass(A, fields=list(X=numeric))
 setRefClass(B, contains=A, fields=list(Y=character))

 mySetAs - function(
 from,
 to
 ){
 if(!existsMethod(f=coerce, signature=c(from=class(from), to=to))){
 setAs(from=class(from), to=to,
  def=function(from, to){
 out - getRefClass(to)$new(X=from)
 return(out)
 }
 )
 }
  mthd - selectMethod(coerce, signature=c(from=class(from),
 to=to), useInherited= c(from=TRUE, to=TRUE))
 out - mthd(from=from, to=to)
 return(out)
 }

 a - mySetAs(from=1:5, to=A)
 a$X
 b - mySetAs(from=1:5, to=B)
 b$X

 I'm sure there are much better ways. I'd appreciate any comments
 whatsoever.

 Best regards,
 Janko

 On 06.06.2011 17:46, Janko Thyson wrote:
  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]]

 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide
 http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.


[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] How can I write methods for 'as()'?

2011-06-06 Thread Janko Thyson
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.thy...@googlemail.com mailto:janko.thy...@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]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] How can I write methods for 'as()'?

2011-06-06 Thread Janko Thyson
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]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] How can I write methods for 'as()'?

2011-06-06 Thread Janko Thyson
Okay, I found something that is working, but it looks and feels pretty 
awkward as the method def and method lookup takes place in one function ;-)

setRefClass(A, fields=list(X=numeric))
setRefClass(B, contains=A, fields=list(Y=character))

mySetAs - function(
 from,
 to
){
 if(!existsMethod(f=coerce, signature=c(from=class(from), to=to))){
 setAs(from=class(from), to=to,
 def=function(from, to){
 out - getRefClass(to)$new(X=from)
 return(out)
 }
 )
 }
 mthd - selectMethod(coerce, signature=c(from=class(from), 
to=to), useInherited= c(from=TRUE, to=TRUE))
 out - mthd(from=from, to=to)
 return(out)
}

a - mySetAs(from=1:5, to=A)
a$X
b - mySetAs(from=1:5, to=B)
b$X

I'm sure there are much better ways. I'd appreciate any comments whatsoever.

Best regards,
Janko

On 06.06.2011 17:46, Janko Thyson wrote:
 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]]

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.