What does this mean: unable to find an inherited method for function ‘A’ for signature ‘“B”’

前端 未结 2 1008
清歌不尽
清歌不尽 2020-12-03 10:56

I am new to R and keep getting errors with the following message:

unable to find an inherited method for function ‘A’ for signature ‘\"B\"’

相关标签:
2条回答
  • 2020-12-03 11:03

    That is the type of message you will get when attempting to apply an S4 generic function to an object of a class for which no defined S4 method exists (or at least has been attached to the current R session).

    Here's an example using the raster package (for spatial raster data), which is chock full of S4 functions.

    library(raster)
    
    ## raster::rotate() is an S4 function with just one method, for "Raster" class objects
    isS4(rotate)
    # [1] TRUE
    showMethods(rotate)
    # Function: rotate (package raster)
    # x="Raster"
    
    ## Lets see what happens when we pass it an object that's *not* of class "Raster"
    x <- 1:10
    class(x)
    # [1] "integer"
    rotate(x)
    # Error in (function (classes, fdef, mtable)  : 
    #   unable to find an inherited method for function ‘rotate’ for signature ‘"integer"’
    
    0 讨论(0)
  • 2020-12-03 11:17

    I have seen this message numerous times, as a result of namespace conflicts.

    Here is a MRE: Both the hash and data.table libraries have copy functions.

    In a new R session:

    > library(data.table)
    > library(hash)
    

    causes copy from data.table to be masked:

    > DT = data.table(x=rep(c("b","a","c"),each=3), y=c(1,3,6), v=1:9)
    > copy(DT)
    Error in (function (classes, fdef, mtable)  : 
      unable to find an inherited method for function ‘copy’ for signature ‘"data.table"’
    

    The solution is to specify the namespace:

    > data.table::copy(DT)
       x y v
    1: b 1 1
    2: b 3 2
    3: b 6 3
    4: a 1 4
    5: a 3 5
    6: a 6 6
    7: c 1 7
    8: c 3 8
    9: c 6 9
    
    0 讨论(0)
提交回复
热议问题