5

I'm trying to open an Excel file with the read_excel() function from the readxl package. But I don't know how to specify the path to the Excel file.

When I enter the file path by following the steps given in the answer, I receive the error:

"Error: std::bad_alloc".

Sam Firke
  • 21,571
  • 9
  • 87
  • 105
ribery77
  • 141
  • 1
  • 3
  • 8

6 Answers6

4

First load the package:

library(readxl)

According to the package development page, you just have to specify the file name as a string, for example:

read_excel("my-old-spreadsheet.xls")
read_excel("my-new-spreadsheet.xlsx")

You can also specify the name or number of sheet:

read_excel("my-spreadsheet.xls", sheet = "data")
read_excel("my-spreadsheet.xls", sheet = 2)

Make sure you are in the correct directory using getwd(). If not, change it using setwd()

Paulo MiraMor
  • 1,582
  • 12
  • 30
2

As noted in the comments, that error message is caused by an issue with the file size. See the package issue at: https://github.com/hadley/readxl/issues/150.

Sam Firke
  • 21,571
  • 9
  • 87
  • 105
1

Just encountered this problem, when re-reading a file I read successfully!

As vladdsm points out on github, closing and re-opening Rstudio can solve the problem https://github.com/tidyverse/readxl/issues/150#issuecomment-236883769

psychonomics
  • 714
  • 4
  • 12
  • 26
0

In my case I have opened the file in LibreOffice Calc (you could use Excel of course but I don't own that), removed the complex header rows containing merged cells and colored backgrounds etc, simplified it manually into a 1-row header, then saved the file. The new Excel file was much smaller, and Importing in with readxl::read_excel() was possible now.

Perhaps the software cleaned up the file internally in a different manner as well.

Of course this works only if importing into R is a one-off task, a single Excel file with a few sheets at most.

knb
  • 9,138
  • 4
  • 58
  • 85
0

I had the same issue while trying to read xlsx file into R environment. The file size is not large. The issue was resolved after i have reinstalled readxl package.

coding_is_fun
  • 117
  • 1
  • 14
-1

I guess its a problem with your memory. Maybe you had other programs open, so it was not enough memory to load and process the datafile in R. This might occur, if the datafile is large.

You can easily check that in you task manager while trying to read the xlsx file.

regards Basti