R中`subset`方法的多重分派 [英] Multiple dispatch for `subset` methods in R

查看:90
本文介绍了R中`subset`方法的多重分派的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个程序包,并希望为自定义类myclass的对象编写两个子集方法,并分派两个参数,第一个是类myclass的子集对象,第二个是字符向量的两种逻辑,如下所示:

I am developing a package and want to write two subset methods for objects of a custom class, myclass, with dispatch on two arguments, first one being the object to subset, of class myclass, and the second being either logical of character vector, like so:

setMethod(
    f = "subset",
    signature = c(x = "myclass", subset = "logical"),
    definition = function(x, subset){
            # function body 
    }
)

setMethod(
    f = "subset",
    signature = c(x = "myclass", subset = "character"),
    definition = function(x, subset){ 
        # different function body 
    }
)

但是,我无法执行此操作,因为S3泛型仅在一个参数上进行分派.而且我不想为subset创建新的泛型,因为它会在加载我的包时掩盖现有的泛型. 我认为,解决此问题的一种方法是创建一个通用名称和名称不同的方法,但这对用户来说不是很直观,对吧? 那么我是否会遗漏/误解某些东西,并且有任何巧妙的方法可以对S3泛型进行多次调度?

However, I cannot do this because the S3 generic dispatches on one argument only. And I don't want to create a new generic for subset because it would mask the existing generic when my package is loaded. One way around this issue, I think, would be to create a generic and methods of different name, but this would not be very intuitive for the users, right? So am I missing/misunderstanding something, and is there any witty way to have multiple dispatch for S3 generics?

推荐答案

通常在这种情况下,您可以将subset设置为S4泛型,但是由于您有不想这样做的理由,可以通过定义来解决此问题.一个单独的泛型,然后从S3方法中调用,沿

Normally in this situation you would set subset as an S4 generic but since you have reasons for not wanting to do that, you can get around this by defining a separate generic and calling it from within the S3 method, along the lines of

mySubset <- function(x,subset){
  stop("this is only a generic function: it should never be called!")
}
setGeneric("mySubset")
## methods for mySubset
setMethod(
    f = "mySubset",
    signature = c(x = "myclass", subset = "logical"),
    definition = function(x, subset){
            # function body 
    }
)

setMethod(
    f = "mySubset",
    signature = c(x = "myclass", subset = "character"),
    definition = function(x, subset){ 
        # different function body 
    }
)

## default method using "ANY" (lower priority)
setMethod(
    f = "mySubset",
    signature = c(x = "myclass", subset = "ANY"),
    definition = function(x, subset){ 
       ## insert default behaviour (might be an error),
       ## a call to subset.default or whatever
    }
)

## now write an S3 method for subset that calls the S4 generic if
## x is of class myclass

subset.myClass <- function(x,subset){
  mySubset(x,subset)
}

这保留了子集的仅S3行为,但是现在只要x属于类myclass,您就可以对方法分派进行S4级别的控制.

This preserves the S3-only behaviour of subset, but you now have S4 level control over method dispatch provided that x is of class myclass.

您的用户不需要欣赏这种区别;当x拥有您的新班级时,他们仍然可以按照习惯的方式调用subset(x,class).

Your users don't need to appreciate this distinction; they can still call subset(x,class) in the same way they are accustomed to, when x has your new class.

这篇关于R中`subset`方法的多重分派的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆