Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error reading XLSX file in R

Tags:

java

r

I'm trying to read an 1 MB XLSX file stored on a network drive using the lines

options(java.parameters="-Xmx4g")
library(rJava) 
library(xlsx) 
jgc <- function() {
  gc()
  .jcall("java/lang/System", method = "gc")
}    
jgc()
folder <- 'network_drive/State_Stuff/'  
state <- 'MI'
xlsx_in<- read.xlsx2(paste(folder, state,"_data.xlsx", sep = ""),
                  sheetName = "sheet1",
                  colClasses = c(rep("numeric",8), "character"))

And I get the following error.

Error in .jcall("RJavaTools", "Ljava/lang/Object;", "invokeMethod", cl, : org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException

I'm running the Windows7 64-bit RStudio, and running java -version returns

java version "1.8.0_111"
Java(TM) SE Runtime Environment (build 1.8.0_111-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)

I've tried copy/pasting the file onto my desktop from the network drive and reading that, but that throws the same error.

However, moving the file to my desktop, and using Save-As, and saving it as a new file allows me to read it just fine.

Does anyone have an idea on what's going on here?

like image 972
SharpObject Avatar asked Feb 06 '23 13:02

SharpObject


1 Answers

Try using the readxl package. It doesn't depend on java.

So for example:

library(readxl) 

folder <- 'network_drive/State_Stuff/'  
state <- 'MI'
xlsx_in<- read_excel(paste(folder, state,"_data.xlsx"),
                  sheet = "sheet1",
                  col_types = c(rep("numeric",8), "text"))
like image 101
boshek Avatar answered Feb 15 '23 02:02

boshek