[Rd] how to define method for "+" function in a new class

2010-07-07 Thread james.foadi


Dear R developers,
I have a new class, which I called "Molecule", and have tried to define =
a "+" operation for 2 objects of this class.
This is what I have written so far, although the method is not complete =
(I'm trying to look at it at intermediate stages):

setMethod(
  f=3D"+",
  signature(x=3D"Molecule",y=3D"Molecule"),
  definition=3Dfunction(x,y,...)
 {
  # Check both objects are correct
  checkMolecule(x)
  checkMolecule(y)

  # Extract chains information
  ch1 <- getMoleculeChains(x)
  ch2 <- getMoleculeChains(y)
  union_ch <- unique(c(ch1,ch2))
  not_used <- .ALPHABET
  for (i in seq(along =3D union_ch))
  {
   idx <- which(not_used !=3D union_ch[i])
   if (length(idx) !=3D 0) not_used <- not_used[idx]
  }

  return(not_used)
 }
 )


The definition of class Molecule is included earlier in the same file. =
When I source it, I get the following error message:

Error in match.call(fun, fcall) :=20
  unused argument(s) (x =3D "Molecule", y =3D "Molecule")


I can't see what's wrong in my method definition. Can anyone help me =
with this?

J

Dr James Foadi PhD
Membrane Protein Laboratory (MPL)
Diamond Light Source Ltd
Diamond House
Harewell Science and Innovation Campus
Chilton, Didcot
Oxfordshire OX11 0DE

Email:  james.fo...@diamond.ac.uk
Alt Email:  j.fo...@imperial.ac.uk

-- 
This e-mail and any attachments may contain confidential...{{dropped:8}}

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] how to define method for "+" function in a new class

2010-07-07 Thread james.foadi
I don't know why a "3D" appears instead of a "(".
Please, interpret a "( whenever you see a "3D".

J

Dr James Foadi PhD
Membrane Protein Laboratory (MPL)
Diamond Light Source Ltd
Diamond House
Harewell Science and Innovation Campus
Chilton, Didcot
Oxfordshire OX11 0DE

Email:  james.fo...@diamond.ac.uk
Alt Email:  j.fo...@imperial.ac.uk



-Original Message-
From: r-devel-boun...@r-project.org on behalf of james.fo...@diamond.ac.uk
Sent: Wed 07/07/2010 16:09
To: r-devel@r-project.org
Subject: [Rd] how to define method for "+" function in a new class
 


Dear R developers,
I have a new class, which I called "Molecule", and have tried to define =
a "+" operation for 2 objects of this class.
This is what I have written so far, although the method is not complete =
(I'm trying to look at it at intermediate stages):

setMethod(
  f=3D"+",
  signature(x=3D"Molecule",y=3D"Molecule"),
  definition=3Dfunction(x,y,...)
 {
  # Check both objects are correct
  checkMolecule(x)
  checkMolecule(y)

  # Extract chains information
  ch1 <- getMoleculeChains(x)
  ch2 <- getMoleculeChains(y)
  union_ch <- unique(c(ch1,ch2))
  not_used <- .ALPHABET
  for (i in seq(along =3D union_ch))
  {
   idx <- which(not_used !=3D union_ch[i])
   if (length(idx) !=3D 0) not_used <- not_used[idx]
  }

  return(not_used)
 }
 )


The definition of class Molecule is included earlier in the same file. =
When I source it, I get the following error message:

Error in match.call(fun, fcall) :=20
  unused argument(s) (x =3D "Molecule", y =3D "Molecule")


I can't see what's wrong in my method definition. Can anyone help me =
with this?

J

Dr James Foadi PhD
Membrane Protein Laboratory (MPL)
Diamond Light Source Ltd
Diamond House
Harewell Science and Innovation Campus
Chilton, Didcot
Oxfordshire OX11 0DE

Email:  james.fo...@diamond.ac.uk
Alt Email:  j.fo...@imperial.ac.uk

-- 
This e-mail and any attachments may contain confidential...{{dropped:16}}

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] how to define method for "+" function in a new class

2010-07-07 Thread james.foadi
Thank you, Martin! Very clear.

J

Dr James Foadi PhD
Membrane Protein Laboratory (MPL)
Diamond Light Source Ltd
Diamond House
Harewell Science and Innovation Campus
Chilton, Didcot
Oxfordshire OX11 0DE

Email:  james.fo...@diamond.ac.uk
Alt Email:  j.fo...@imperial.ac.uk



-Original Message-
From: Martin Morgan [mailto:mtmor...@fhcrc.org]
Sent: Wed 07/07/2010 16:43
To: Foadi, James (Imperial Coll.,RAL,DIA)
Cc: r-devel@r-project.org
Subject: Re: [Rd] how to define method for "+" function in a new class
 
On 07/07/2010 08:09 AM, james.fo...@diamond.ac.uk wrote:
> 
> 
> Dear R developers,
> I have a new class, which I called "Molecule", and have tried to define =
> a "+" operation for 2 objects of this class.
> This is what I have written so far, although the method is not complete =
> (I'm trying to look at it at intermediate stages):
> 
> setMethod(
>   f=3D"+",
>   signature(x=3D"Molecule",y=3D"Molecule"),
>   definition=3Dfunction(x,y,...)
>  {
>   # Check both objects are correct
>   checkMolecule(x)
>   checkMolecule(y)
> 
>   # Extract chains information
>   ch1 <- getMoleculeChains(x)
>   ch2 <- getMoleculeChains(y)
>   union_ch <- unique(c(ch1,ch2))
>   not_used <- .ALPHABET
>   for (i in seq(along =3D union_ch))
>   {
>idx <- which(not_used !=3D union_ch[i])
>if (length(idx) !=3D 0) not_used <- not_used[idx]
>   }
> 
>   return(not_used)
>  }
>  )
> 
> 
> The definition of class Molecule is included earlier in the same file. =
> When I source it, I get the following error message:
> 
> Error in match.call(fun, fcall) :=20
>   unused argument(s) (x =3D "Molecule", y =3D "Molecule")

If I

> getGeneric("+")
standardGeneric for "+" defined from package "base"
  belonging to group(s): Arith

function (e1, e2)
standardGeneric("+", .Primitive("+"))

Methods may be defined for arguments: e1, e2
Use  showMethods("+")  for currently available ones.

I see that the generic is defined to take two arguments e1 and e2. So

setMethod("+", c("Molecule", "Molecule"), function(e1, e2) {
## ...
})

but actually here it might often pay to discover ?GroupGenericFunctions
and end up with something like


setClass("A", representation=representation(x="numeric"))

setMethod("Arith", c("A", "A"), function(e1, e2) {
   new(class(e1), x=callGeneric(e1...@x, e2...@x))
})

and then

> new("A", x=1:5) + new("A", x=5:1)
An object of class "A"
Slot "x":
[1] 6 6 6 6 6

but also

> new("A", x=1:5) * new("A", x=5:1)
An object of class "A"
Slot "x":
[1] 5 8 9 8 5

Martin

> 
> 
> I can't see what's wrong in my method definition. Can anyone help me =
> with this?
> 
> J
> 
> Dr James Foadi PhD
> Membrane Protein Laboratory (MPL)
> Diamond Light Source Ltd
> Diamond House
> Harewell Science and Innovation Campus
> Chilton, Didcot
> Oxfordshire OX11 0DE
> 
> Email:  james.fo...@diamond.ac.uk
> Alt Email:  j.fo...@imperial.ac.uk
> 


-- 
Martin Morgan
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109

Location: Arnold Building M1 B861
Phone: (206) 667-2793


-- 
This e-mail and any attachments may contain confidential...{{dropped:8}}

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] method using several (and different) arguments in turn

2012-02-14 Thread james.foadi
Dear R-developers community, I have the following generic:

setGeneric(
   name="newsample",
   def=function(x,y,z,a,b,c,...){standardGeneric("newsample")}

And I can build several methods for this generic. One useful thing is to use 
"newsample"
with only one of the 6 arguments listed. At the moment this is what I do:

setMethod(
   f="newsample",
   
signature=c("missing","missing","numeric","missing","missing","missing"),
   function(x,y,z,a,b,c,...)
   {
   ..
   ..

}
)

This would be used when the single argument is z:

newsample(z=12.5)

To use newsample with another argument (say x) I should implement the same as 
before,
but with signature 
c("numeric","missing","missing","missing","missing","missing").
Is there another shorter and easier way to do this?


J

-- 
This e-mail and any attachments may contain confidential...{{dropped:8}}

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] method using several (and different) arguments in turn

2012-02-14 Thread james.foadi
Dear Martin,
I could not entirely follow your suggestion.
I can see how you define two classes inheriting from TypeOfSample, and how 
these two
classes have two associated methods (incidentally, I'm unfamiliar with the:

   function(type,x=numeric(),...)to do.

J


expression. What is type?

But then I'm lost. I wouldn't know how to carry on from here. 

I don't know if I have explained the key point clear enough before. For the 
person using
"newsample" it makes sense which name (x or y or z, etc) is used. 
newsample(x=12.4) would
give something different from newsample(y=12.4); and yet another result would 
be obtained if
using a combination, like newsample(x=12.4,y=12.4).

I wanted to use a simple function with default values at first, but I'm in the 
middle of developing
a package using S4 formalism. I'm not sure this would be a wise thing 

Sent: 14 February 2012 17:00
To: Foadi, James (Imperial Coll.,RAL,DIA)
Cc: r-devel@r-project.org
Subject: Re: [Rd] method using several (and different) arguments in turn

On 02/14/2012 08:43 AM, james.fo...@diamond.ac.uk wrote:
> Dear R-developers community, I have the following generic:
>
> setGeneric(
> name="newsample",
> 
> def=function(x,y,z,a,b,c,...){standardGeneric("newsample")}
>
> And I can build several methods for this generic. One useful thing is to use 
> "newsample"
> with only one of the 6 arguments listed. At the moment this is what I do:
>
> setMethod(
> f="newsample",
> 
> signature=c("missing","missing","numeric","missing","missing","missing"),
> function(x,y,z,a,b,c,...)
> {
> ..
> ..
>
>  }
>  )
>
> This would be used when the single argument is z:
>
> newsample(z=12.5)
>
> To use newsample with another argument (say x) I should implement the same as 
> before,
> but with signature 
> c("numeric","missing","missing","missing","missing","missing").
> Is there another shorter and easier way to do this?

Hi James --

A matter of opinion, but multiple dispatch like this can be very
complicated, e.g., figuring out the 'next' method when dispatching on
two or more arguments; I'd really discourage it.

A different approach, assuming that x, y, z, ... are all numeric() but
that the sample to be drawn differs, is to define a small class
hierarchy to be used for dispatch.

   setClass("TypeOfSample")

   setClass("XSample", contains="TypeOfSample")
   XSample <- new("XSample")  ## a 'singleton', used for dispatch

   setClass("YSample", contains="TypeOfSample")
   YSample <- new("YSample")

and then

   setGeneric("newsample",
   function(type, x=numeric(), ...) standardGeneric("newsample"),
   signature="type")

   setMethod("newsample", "XSample", function(type, x=numeric(), ...) {
   "XSample"
   })

   setMethod("newsample", "YSample", function(type, x=numeric(), ...) {
   "YSample"
   })

One could implement a default method on "TypeOfSample", and use
callNextMethod() after initial transformation, if that were the pattern.

To use:

   newsample(XSample, x=1:100)

Martin

>
>
> J
>


--
Computational Biology
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109

Location: M1-B861
Telephone: 206 667-2793

-- 
This e-mail and any attachments may contain confidential...{{dropped:8}}

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel