Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

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

In most cases I've been able to solve my issues by finding alternate examples online, but I'd like to understand what the error message means so I can better understand how R works.

For example, this code:

library("RSQLite") con = dbConnect(drv="SQLite", dbname="database.db") 

Generates this warning:

unable to find an inherited method for function ‘dbConnect’ for signature ‘"character"’

And after fixing that error, this code:

dbClearResult(p1) 

Produces this warning:

unable to find an inherited method for function ‘dbClearResult’ for signature ‘"data.frame"’

Can somebody please explain what this type of error message is trying to tell me?

Specifically, the terms "interhited", "method", "function", and "signature" all seem related to concepts I understand from other languages, but the sentence structure of this error implies they have slightly different meanings in R.

like image 704
user12341234 Avatar asked Apr 29 '16 16:04

user12341234


2 Answers

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"’ 
like image 154
Josh O'Brien Avatar answered Oct 07 '22 08:10

Josh O'Brien


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 
like image 25
James Hirschorn Avatar answered Oct 07 '22 07:10

James Hirschorn