[R-pkg-devel] [External] Re: Farming out methods to other packages

Lenth, Russell V ru@@e||-|enth @end|ng |rom u|ow@@edu
Sat Aug 10 22:26:46 CEST 2019


Thanks, Luke. 

That's more efficient for sure. I'd just have to rename about a zillion existing internal methods -- but only once!

Additional advantage I see is ability to control WHICH methods I'd allow to be overridden.

Russ

-----Original Message-----
From: Tierney, Luke <luke-tierney using uiowa.edu> 
Sent: Saturday, August 10, 2019 3:15 PM
To: Lenth, Russell V <russell-lenth using uiowa.edu>
Cc: Duncan Murdoch <murdoch.duncan using gmail.com>; Iñaki Ucar <iucar using fedoraproject.org>; r-package-devel using r-project.org
Subject: Re: [R-pkg-devel] [External] Re: Farming out methods to other packages

You could have your default method handle the cases you can handle; if you want that to dispatch you can use something like

recover_data.default <- function(object, ...)
     default_recover_data(object, ...)
default_recover_data <- function(object, ...)
     UseMethod("default_recover_data")

Best,

luke

On Sat, 10 Aug 2019, Lenth, Russell V wrote:

> Thanks, Duncan. That's helpful.
>
> In addition, I confess I had a closing parenthesis in the wrong place.
>    Should be:    . . .   .GlobalEnv), silent = TRUE)
>
> Cheers,
>
> Russ
>
> -----Original Message-----
> From: Duncan Murdoch <murdoch.duncan using gmail.com>
> Sent: Saturday, August 10, 2019 2:43 PM
> To: Lenth, Russell V <russell-lenth using uiowa.edu>; Iñaki Ucar 
> <iucar using fedoraproject.org>
> Cc: r-package-devel using r-project.org
> Subject: Re: [R-pkg-devel] [External] Re: Farming out methods to other 
> packages
>
> On 10/08/2019 3:27 p.m., Lenth, Russell V wrote:
>> Hmmmm, I thought of an approach -- a kind of manual dispatch 
>> technique. My generic is
>>
>> recover_data <- function(object, ...) {
>>      rd <- try(getS3method("recover_data", class(object)[1], envir = .GlobalEnv, silent = TRUE))
>>      if (!inherits(rd, "try-error"))
>>          rd(object, ...)
>>      else
>>          UseMethod("recover_data")
>> }
>>
>> and similar for emm_basis. The idea is it tries to find the method among globally registered ones, and if so, it uses it; otherwise, the internal one is used.
>
> That's a bad test:  class(object) might be a vector c("nomethod", "hasmethod").  You're only looking for recover_data.nomethod, and maybe only recover_data.hasmethod exists.
>
> The getS3method() function won't automatically iterate through the 
> class, you'll need to do that yourself, for example
>
> S3methodOrDefault <- function(object, generic, default) {
>   for (c in class(object)) {
>     rd <- try(getS3method(generic, c, envir = .GlobalEnv, silent = TRUE))
>     if (!inherits(rd, "try-error"))
>       return(rd)
>   }
>   return(default)
> }
>
> used as
>
>   S3methodOrDefault(object, "recover_data", internal_recover_data)
>
> Duncan Murdoch
>
> ______________________________________________
> R-package-devel using r-project.org mailing list 
> https://stat.ethz.ch/mailman/listinfo/r-package-devel

--
Luke Tierney
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa                  Phone:             319-335-3386
Department of Statistics and        Fax:               319-335-3017
    Actuarial Science
241 Schaeffer Hall                  email:   luke-tierney using uiowa.edu
Iowa City, IA 52242                 WWW:  http://www.stat.uiowa.edu


More information about the R-package-devel mailing list