3

Im trying to read an excel file into R. It's about the following file in my cwd:

 > list.files()
 [1] "Keuren_Op_Afspraak.xlsx"

I installed XLConnect and am doing the following:

library(XLConnect)
demoExcelFile <- system.file("Keuren_Op_Afspraak.xlsx", package = "XLConnect")
wb <- loadWorkbook(demoExcelFile)

But this gives me the error:

Error: FileNotFoundException (Java): File '' could not be found - you may specify to automatically create the file if not existing.

But I dont understand where this is coming from. Any thoughts?

Marc van der Peet
  • 323
  • 1
  • 6
  • 14
  • 1
    possible duplicate of [Importing .xlsx file into R](http://stackoverflow.com/questions/7049272/importing-xlsx-file-into-r) –  Jul 08 '15 at 08:30

3 Answers3

5

I prefer using the readxl package. It is written in C so it is faster. It also seems to handle large files better. The command would be:

library(readxl)
wb <- read_excel("Keuren_Op_Afspraak.xlsx")
Steve Rowe
  • 19,411
  • 9
  • 51
  • 82
3

You can also use the xlsx package.

library(xlsx)
wb <- read.xlsx("Keuren_Op_Afspraak.xlsx", sheet = 1)

Edit :@Verena

You can also use this function much faster:

wb <- read.xlsx2("Keuren_Op_Afspraak.xlsx", sheet = 1)
Leonhardt Guass
  • 773
  • 7
  • 24
Mostafa90
  • 1,674
  • 1
  • 21
  • 39
2

You have to change your code that way:

library(XLConnect)
demoExcelFile <- "Keuren_Op_Afspraak.xlsx"
wb <- loadWorkbook(demoExcelFile)

You probably took the example from here: http://www.inside-r.org/packages/cran/XLConnect/docs/loadWorkbook

This line

system.file("demoFiles/mtcars.xlsx", package = "XLConnect")

is a way to get sample files that are part of a package. If you download the zip File of XLConnect and look into the folder structure you will see that there is a folder demoFiles that contains mtcars.xlsx. And the parameter package="XLConnect" tells the method to look for the file in this package.

If you type it into the command line it returns the absolute path to the file:

"C:/Users/Expecto/Documents/R/win-library/3.1/XLConnect/demoFiles/mtcars.xlsx"

To use loadWorkbook you simply need to pass the relative or absolute filepath.

Verena Haunschmid
  • 1,252
  • 15
  • 40