Error in XLConnect

salhin picture salhin · Oct 27, 2014 · Viewed 13.2k times · Source

I am trying to import an excel sheet into r. I used the following code:

x <- loadWorkbook("x.xlsx")
b <- readWorksheet(x, sheet="b")

The first line works fine, however, running the second gives the following error:

Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘readWorksheet’ for signature ‘"jobjRef", "character"’

I have no missing values in that sheet.

For the purpose of reproducing, download trial.xlsx from https://github.com/ahmedfsalhin/1stpaper.

system info: Yosemite operating system.

Answer

Carl Witthoft picture Carl Witthoft · Oct 28, 2014

It appears the "root cause" is that you should add code to specify both the function and the package it belongs to. Type XLConnect::loadWorkbook to select the one you want in this case. There's no 'confusion,' or random selection of duplicated function names in R. The choice depends on the load order of all loaded packages. Use search() to see the order in which packages are checked for the command you've entered.

E.g., at present I get

search()
 [1] ".GlobalEnv"            "package:caTools"      
 [3] "package:XLConnect"     "package:XLConnectJars"
 [5] "package:stats"         "package:graphics"     
 [7] "package:datasets"      "package:vecsets"      
 [9] "package:cgwtools"      "package:grDevices"    
[11] "package:utils"         "package:methods"      
[13] "Autoloads"             "package:base"

You'll notice that anything in your environment (.GlobalEnv) is selected first, and that all loaded libraries override the base package, for example.