Problems passing arguments with callNextMethod() in R

前端 未结 1 1115
眼角桃花
眼角桃花 2021-01-12 12:35

My question:

Why is callNextMethod() not passing arguments as expected to the next method?

Situation:

Say I have two h

相关标签:
1条回答
  • 2021-01-12 13:27

    I think this has to do with the way a method with a signature different from the generic is defined (within a function .local)

    > selectMethod(foobar, "bar")
    Method Definition:
    
    function (object, ...) 
    {
        .local <- function (object, another.argument = FALSE, ...) 
        {
            print(paste("in bar-method:", another.argument))
            object@x <- sqrt(object@x)
            callNextMethod()
        }
        .local(object, ...)
    }
    
    Signatures:
            object
    target  "bar" 
    
    defined "bar" 
    

    The work-around is to either define the generic and methods to have the same signature

    setGeneric("foobar",
        function(object, another.argument=FALSE, ...) standardGeneric("foobar"),
        signature="object")
    

    or pass the arguments explicitly to callNextMethod

    setMethod("foobar", "bar", function(object, another.argument = FALSE, ...) {
        print(paste("in bar-method:", another.argument))
         object@x <- sqrt(object@x)
        callNextMethod(object, another.argument, ...)
    })
    
    0 讨论(0)
提交回复
热议问题