Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
683 views
in Technique[技术] by (71.8m points)

methods - R hangs when there are too many arguments in setMethod (or setGeneric)

Basically, when there are many arguments in setMethod or (setGeneric) it works very slowly.

Here is a basic example:

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"))

I observe that this is not related to the class. You can put a numeric class into r and it will do the same. If I trim the number of arguments it will work.

I suspect that it tries "to find an inherited method for function ‘foo’ for signature ‘"numeric", "classA", "missing", ..." and this causes R to hang. HERE is a good discussion of this topic.

Because if I run same code with less parameters it works:

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"))

Why this happens? Any ideas will be appreciated.

question from:https://stackoverflow.com/questions/25574510/r-hangs-when-there-are-too-many-arguments-in-setmethod-or-setgeneric

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I had a similar problem. S4 seems the all possible combinations of signature. Which slows things down to a crawl. The solution is to have as little elements in the signature as possible.

You can find the details here https://stat.ethz.ch/pipermail/r-devel/2015-May/071092.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...