John Smith John Smith - 2 months ago 34
R Question

Using XLConnect to read in xls

I'm using the following code to try and read in an old

xls
file

library("XLConnect")
path <- "C:/Users/foo/Desktop/WEEK 17.xls"
df <- readWorksheet(path, sheet = 1)


i get the following error


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


Does anyone know why?
I can open the file in excel

Answer

It s because readWorksheet use workbook as object.

You have to

# Load workbook
wb <- loadWorkbook("C:/Users/foo/Desktop/WEEK 17.xls")

and then

df <- readWorksheet(wb, sheet = 1)

or use readWorksheetFromFile("C:/Users/foo/Desktop/WEEK 17.xls", sheet = 1)

Which realy do the same

> XLConnect::readWorksheetFromFile
function (file, ...) 
{
    args <- list(...)
    args$object <- loadWorkbook(file, create = FALSE)
    do.call("readWorksheet", args)
}
<environment: namespace:XLConnect>
Comments