[R-pkg-devel] Creating Accessor Methods

2016-03-10 Thread Glenn Schultz
All,

I have a package with to S4 classes (MBSCashFlow, REMICCashFlow and 
BondCashFlow) all of which contain the slot Duration.  I would like have an 
accessor Duration that would work on multiple signatures.  Is this possible?  I 
have checked my books, help, BioConductor tutorials and it appears that this is 
not possible.  Has anyone ever run into this situation?

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


Re: [R-pkg-devel] Creating Accessor Methods

2016-03-10 Thread Joris Meys
Hi Glenn,

that is actually as easy as defining a generic and defining a method for
each class to extract the slot. I do this literally in every package I
write.

setGeneric('duration', function(x, ...) standardGeneric('duration')

setMethod('duration', signature = 'MBSCashFlow', function(x) { x@Duration})
setMethod('duration', signature = 'REMICCashFlow', function(x) {x@Duration})
etc...

I suggest you take a close look at Hadley Wickham's books 'Advanced R'  and
'R packages', both freely available online.

This is the relevant section of the first book :
http://adv-r.had.co.nz/OO-essentials.html
This is the other book : http://r-pkgs.had.co.nz/

Cheers
Joris

On Thu, Mar 10, 2016 at 3:05 PM, Glenn Schultz  wrote:

> All,
>
> I have a package with to S4 classes (MBSCashFlow, REMICCashFlow and
> BondCashFlow) all of which contain the slot Duration.  I would like have an
> accessor Duration that would work on multiple signatures.  Is this
> possible?  I have checked my books, help, BioConductor tutorials and it
> appears that this is not possible.  Has anyone ever run into this situation?
>
> Best Glenn
> __
> R-package-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-package-devel
>



-- 
Joris Meys
Statistical consultant

Ghent University
Faculty of Bioscience Engineering
Department of Mathematical Modelling, Statistics and Bio-Informatics

tel : +32 9 264 59 87
joris.m...@ugent.be
---
Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php

[[alternative HTML version deleted]]

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


Re: [R-pkg-devel] Creating Accessor Methods

2016-03-10 Thread Daniel Nüst

Hi Glenn!

On 10/03/16 15:05, Glenn Schultz wrote:

I have a package with to S4 classes (MBSCashFlow, REMICCashFlow and 
BondCashFlow) all of which contain the slot Duration.  I would like have an 
accessor Duration that would work on multiple signatures.  Is this possible?  I 
have checked my books, help, BioConductor tutorials and it appears that this is 
not possible.  Has anyone ever run into this situation?


I actually use a similar mechanism to extract values from different S4 
classes in my package, and it works quite well. So if I understand you 
right, the following code might help you to get closer to want you want:


https://github.com/52North/sos4R/blob/master/R/SOS-methods-accessor.R

I created methods for different signatures, see for example 
"sosObservedProperties".



Hope this helps,
Daniel

--
Daniel Nüst
Institute for Geoinformatics (ifgi),   University of Münster
Heisenbergstraße 2, 48149 Münster, Germany; +49 251 83 31962
http://ifgi.uni-muenster.de/~d_nues01http://o2r.info

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


Re: [R-pkg-devel] Creating Accessor Methods

2016-03-10 Thread Thomas Petzoldt

On 10.03.2016 16:48, Daniel Nüst wrote:

Hi Glenn!

On 10/03/16 15:05, Glenn Schultz wrote:

I have a package with to S4 classes (MBSCashFlow, REMICCashFlow and
BondCashFlow) all of which contain the slot Duration.  I would like
have an accessor Duration that would work on multiple signatures.  Is
this possible?  I have checked my books, help, BioConductor tutorials
and it appears that this is not possible.  Has anyone ever run into
this situation?


I actually use a similar mechanism to extract values from different S4
classes in my package, and it works quite well. So if I understand you
right, the following code might help you to get closer to want you want:

https://github.com/52North/sos4R/blob/master/R/SOS-methods-accessor.R

I created methods for different signatures, see for example
"sosObservedProperties".


Yes, that's straightforward. But I still wonder if it is at all possible 
to use not only different *signatures*, but also different argument 
*names* in S4, e.g.:


foo(formula, data, ...)

foo(data, grouping, ...)


Now I'm using a combination of S3 and S4 in my package for this purpose, 
and for some other quirks.


Thomas


--
Dr. Thomas Petzoldt
Technische Universitaet Dresden
Faculty of Environmental Sciences
Institute of Hydrobiology
01062 Dresden, Germany

E-Mail: thomas.petzo...@tu-dresden.de
http://tu-dresden.de/Members/thomas.petzoldt

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


Re: [R-pkg-devel] Creating Accessor Methods

2016-03-10 Thread Glenn Schultz
Hello Joris,

Thank worked perfectly.  My main concern was that I was creating multiple 
methods which may have the same name but different signature. Just over 
thinking things I guess but I appreciate your time answering my question.

Glenn

> On Mar 10, 2016, at 8:13 AM, Joris Meys  wrote:
> 
> Hi Glenn,
> 
> that is actually as easy as defining a generic and defining a method for each 
> class to extract the slot. I do this literally in every package I write.
> 
> setGeneric('duration', function(x, ...) standardGeneric('duration')
> 
> setMethod('duration', signature = 'MBSCashFlow', function(x) { x@Duration})
> setMethod('duration', signature = 'REMICCashFlow', function(x) {x@Duration})
> etc...
> 
> I suggest you take a close look at Hadley Wickham's books 'Advanced R'  and 
> 'R packages', both freely available online.
> 
> This is the relevant section of the first book : 
> http://adv-r.had.co.nz/OO-essentials.html 
> 
> This is the other book : http://r-pkgs.had.co.nz/ 
> 
> Cheers
> Joris
> 
> On Thu, Mar 10, 2016 at 3:05 PM, Glenn Schultz  > wrote:
> All,
> 
> I have a package with to S4 classes (MBSCashFlow, REMICCashFlow and 
> BondCashFlow) all of which contain the slot Duration.  I would like have an 
> accessor Duration that would work on multiple signatures.  Is this possible?  
> I have checked my books, help, BioConductor tutorials and it appears that 
> this is not possible.  Has anyone ever run into this situation?
> 
> Best Glenn
> __
> R-package-devel@r-project.org  mailing 
> list
> https://stat.ethz.ch/mailman/listinfo/r-package-devel 
> 
> 
> 
> 
> -- 
> Joris Meys
> Statistical consultant
> 
> Ghent University
> Faculty of Bioscience Engineering
> Department of Mathematical Modelling, Statistics and Bio-Informatics
> 
> tel : +32 9 264 59 87
> joris.m...@ugent.be
> ---
> Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php 
> 

[[alternative HTML version deleted]]

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


Re: [R-pkg-devel] Creating Accessor Methods

2016-03-10 Thread Kasper Daniel Hansen
Creating functions that looks like they have the same name, but have
different internals is exactly (one of) the purposes of having methods.

Best,
Kasper

On Thu, Mar 10, 2016 at 4:04 PM, Glenn Schultz  wrote:

> Hello Joris,
>
> Thank worked perfectly.  My main concern was that I was creating multiple
> methods which may have the same name but different signature. Just over
> thinking things I guess but I appreciate your time answering my question.
>
> Glenn
>
> > On Mar 10, 2016, at 8:13 AM, Joris Meys  wrote:
> >
> > Hi Glenn,
> >
> > that is actually as easy as defining a generic and defining a method for
> each class to extract the slot. I do this literally in every package I
> write.
> >
> > setGeneric('duration', function(x, ...) standardGeneric('duration')
> >
> > setMethod('duration', signature = 'MBSCashFlow', function(x) { x@Duration
> })
> > setMethod('duration', signature = 'REMICCashFlow', function(x)
> {x@Duration})
> > etc...
> >
> > I suggest you take a close look at Hadley Wickham's books 'Advanced R'
> and 'R packages', both freely available online.
> >
> > This is the relevant section of the first book :
> http://adv-r.had.co.nz/OO-essentials.html <
> http://adv-r.had.co.nz/OO-essentials.html>
> > This is the other book : http://r-pkgs.had.co.nz/ <
> http://r-pkgs.had.co.nz/>
> >
> > Cheers
> > Joris
> >
> > On Thu, Mar 10, 2016 at 3:05 PM, Glenn Schultz  > wrote:
> > All,
> >
> > I have a package with to S4 classes (MBSCashFlow, REMICCashFlow and
> BondCashFlow) all of which contain the slot Duration.  I would like have an
> accessor Duration that would work on multiple signatures.  Is this
> possible?  I have checked my books, help, BioConductor tutorials and it
> appears that this is not possible.  Has anyone ever run into this situation?
> >
> > Best Glenn
> > __
> > R-package-devel@r-project.org 
> mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-package-devel <
> https://stat.ethz.ch/mailman/listinfo/r-package-devel>
> >
> >
> >
> > --
> > Joris Meys
> > Statistical consultant
> >
> > Ghent University
> > Faculty of Bioscience Engineering
> > Department of Mathematical Modelling, Statistics and Bio-Informatics
> >
> > tel : +32 9 264 59 87
> > joris.m...@ugent.be
> > ---
> > Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php <
> http://helpdesk.ugent.be/e-maildisclaimer.php>
>
> [[alternative HTML version deleted]]
>
> __
> R-package-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-package-devel
>

[[alternative HTML version deleted]]

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