HBa*_*Bat 40 methods r dispatch s4
基本上,当setMethod
or或(setGeneric
)中存在许多参数时,它的工作速度非常慢.
这是一个基本的例子:
setClassUnion(name = "mNumeric", members = c("missing", "numeric"))
setClass(Class = "classA", representation = representation(ID = "character"))
setGeneric("foo", function(r, i, ..., m = 1, D = 1, U = 999, K = 0.005,
E1 = -5, E2 = 5, E3 = 1, E4 = 1, E5 = 1, E6 = 1,
A1 = -5, A2 = 5, A3 = 1, A4 = 1, A5 = 1, A6 = 1)
{standardGeneric ("foo")})
setMethod(f = "foo",
signature = c(r = "ANY", i = "classA", m = "mNumeric", D = "mNumeric",
U = "mNumeric", K = "mNumeric", E1 = "mNumeric", E2 = "mNumeric",
E3 = "mNumeric", E4 = "mNumeric", E5 = "mNumeric", E6 = "mNumeric",
A1 = "mNumeric", A2 = "mNumeric", A3 = "mNumeric", A4 = "mNumeric",
A5 = "mNumeric", A6 = "mNumeric"),
function(r, i, ..., m, D, U, K, E1, E2, E3, E4, E5, E6, A1, A2, A3, A4, A5, A6)
{print("Function can made it here..")})
#Program hangs after the following code. (at least five minutes)
foo(r = 1, i = new("classA", ID = "ID1"))
Run Code Online (Sandbox Code Playgroud)
我发现这与班级无关.你可以把一个numeric
类放进去r
,它会做同样的事情.如果我削减参数的数量,它将起作用.
我怀疑它尝试" to find an inherited method for function ‘foo’ for signature ‘"numeric", "classA", "missing", ...
",这导致R挂起.这里是对这个主题的一个很好的讨论.
因为如果我使用较少的参数运行相同的代码,它会工作
setGeneric("foo", function(r, i, ..., m = 1, D = 1, U = 999, K = 0.005,
E1 = -5, E2 = 5, E3 = 1, E4 = 1)
{standardGeneric ("foo")})
setMethod(f = "foo",
signature = c(r = "ANY", i = "classA", m = "mNumeric", D = "mNumeric",
U = "mNumeric", K = "mNumeric", E1 = "mNumeric", E2 = "mNumeric",
E3 = "mNumeric", E4 = "mNumeric"),
function(r, i, ..., m, D, U, K, E1, E2, E3, E4)
{print("Function can made it here..")})
foo(r = 1, i = new("classA", ID = "ID1"))
Run Code Online (Sandbox Code Playgroud)
为什么会这样?任何想法将不胜感激.
我有类似的问题。S4似乎是所有可能的签名组合。这会减慢速度。解决方案是签名中的元素尽可能少。
您可以在这里找到详细信息https://stat.ethz.ch/pipermail/r-devel/2015-May/071092.html