简体   繁体   English

在 R 中读取 XLSX 文件时出错

[英]Error reading XLSX file in R

I'm trying to read an 1 MB XLSX file stored on a network drive using the lines我正在尝试使用以下行读取存储在网络驱动器上的 1 MB XLSX 文件

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我正在运行 Windows7 64 位 RStudio,并运行java -version返回

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?有没有人知道这里发生了什么?

Try using the readxl package.尝试使用readxl包。 It doesn't depend on java.它不依赖于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"))

I had the same issue today with a file I received from a colleague and came across this post while searching for a solution.我今天从同事那里收到的文件遇到了同样的问题,并在寻找解决方案时遇到了这篇文章。 Switching to a different library is not really a solution, and if you are used to xlsx, readxl has unexpected behaviors.切换到不同的库并不是真正的解决方案,如果你习惯了 xlsx,readxl 会有意想不到的行为。

I was able to solve the problem by opening the file in Excel and resaving it.我能够通过在 Excel 中打开文件并重新保存来解决该问题。 Not sure if the issue is produced by a particular version of Excel or anything else.不确定问题是由特定版本的 Excel 还是其他任何东西产生的。 I can read similar files from the same colleague just fine.我可以很好地读取来自同一位同事的类似文件。

Used gc() (Garbage Collection) and it works.使用 gc() (垃圾收集)并且它有效。

https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/gc https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/gc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM