使用xlsx包将数据从R插入excel的问题
我正在尝试从R创建一个新的excel工作簿,以使用xlsx包保存一些小数据集。出于某种原因,它工作得很好,但我不能再这样做了 创建新工作簿的代码使用xlsx包将数据从R插入excel的问题,r,excel,xlsx,rjava,R,Excel,Xlsx,Rjava,我正在尝试从R创建一个新的excel工作簿,以使用xlsx包保存一些小数据集。出于某种原因,它工作得很好,但我不能再这样做了 创建新工作簿的代码 library("xlsx") library("xlsxjars") library("rJava") file <- "marca_imei.xlsx" wb <- loadWorkbook(file) # The error: # Error in .jcall("RJavaTools", "Ljava/lang/Object;"
library("xlsx")
library("xlsxjars")
library("rJava")
file <- "marca_imei.xlsx"
wb <- loadWorkbook(file)
# The error:
# Error in .jcall("RJavaTools", "Ljava/lang/Object;", "invokeMethod", cl, :
# java.lang.IllegalArgumentException: Your InputStream was neither an OLE2 stream, nor an OOXML stream
问题可能出在Java上,而不是XLConnect。通过在Java站点上进行测试,确保您已经安装了Java——它将确认Java已正确安装。然后确保R知道找到jre.dll的路径或类似文件名的路径,以确定什么是至关重要的 第二,这是我使用了一年的代码,没有收到错误消息。 如果对你有帮助
read.xls <- function(filename, sheetnumber=1, sheetname=NULL, forceConversion=TRUE, startCol=0, stringsAsFactors=TRUE) {
wb <- loadWorkbook(filename)
if (is.null(sheetname)) sheetname = getSheets(wb)[sheetnumber]
df <- readWorksheet(wb, sheet=sheetname, forceConversion=forceConversion, startCol=startCol)
if (stringsAsFactors) {
ischar <- sapply(df, class) == "character"
for (i in 1:length(df)) {
if (ischar[i]) df[,i] <- factor(df[,i])
}
}
df
}
read.xlsMartin
我认为问题在于您正在读取的文件不是有效的.xlsx文件。下面是一个代码示例来重现您的问题。您还可以修改示例以解决此问题。该示例使用来自web的示例数据集(速度摄影机位置巴尔的摩:-))
实际上,第16行是第26行触发的错误的罪魁祸首,该错误生成了您看到的错误
Error in .jcall("RJavaTools", "Ljava/lang/Object;", "invokeMethod", cl, :
`java.lang.IllegalArgumentException: Your InputStream was neither an OLE2 stream, nor an OOXML stream
要再现错误,请下载文件“rows.csv”,当您在第26行调用read.xlsx时,它会触发您看到的错误。要修复此问题,请更改第16行以下载“rows.xlsx”并重新运行以下脚本:
#!/usr/bin/env Rscript
# Ensure Clean Setup...
# Unload packages
if (require(xlsx)) {
detach("package:xlsx", unload=TRUE)
}
if (require(xlsxjars)) {
detach("package:xlsxjars", unload=TRUE)
}
# Delete Environment...
rm(list = ls())
# Delete directory
if (file.exists("data")) {
unlink("./data", recursive = TRUE)
}
# OK - we should be in a base state setup test...
if (!require(xlsx)) {
install.packages("xlsx")
}
if (!file.exists("data")) {
dir.create("data")
}
# Download the file as a CSV file (Deliberate mistake) not a XLSX file
# This causes the error seen when read.xlsx is invoked...
# To fix replace rows.csv with rows.xlsx
if (!file.exists("data/cameras.xlsx")) {
fileUrl <- "https://data.baltimorecity.gov/api/views/dz54-2aru/rows.csv?accessType=DOWNLOAD"
download.file(fileUrl, destfile = "./data/cameras.xlsx", method = "curl")
}
list.files("./data")
# Now we check the file exists and read in the data...
# read.xlsx will throw the java error as the file downloaded is not a valid excel file...
if (!file.exists(".data/cameraData.xlsx")) {
cameraData.xlsx <- read.xlsx("./data/cameras.xlsx", sheetIndex=1, header = TRUE)
}
head(cameraData.xlsx)
无论如何都不是答案,但我更喜欢XLConnect软件包。谢谢你的评论。我来看看软件包。为了排除您的xlsx
文件出现问题,您可以试试wb它是否有效。无论如何,谢谢你的尝试<代码>>wb wb[1]“Java对象{Name:/xl/workbook.xml-内容类型:application/vnd.openxmlformats officedocument.spreadsheetml.sheet.main+xml}”
我最终使用了XLConnect软件包,它运行良好,文档记录良好。感谢您的回答,我将尝试一下。但是,当使用xlsx时会出现错误。我试图读取excel文件,但得到以下结果:>read.xls(“1_-imei.xslx”)在loadWorkbook(文件名)中出错:找不到从以下位置调用的1_-imei.xslx:top-level Browse[1]>c
这就是问题:wb
#!/usr/bin/env Rscript
# Ensure Clean Setup...
# Unload packages
if (require(xlsx)) {
detach("package:xlsx", unload=TRUE)
}
if (require(xlsxjars)) {
detach("package:xlsxjars", unload=TRUE)
}
# Delete Environment...
rm(list = ls())
# Delete directory
if (file.exists("data")) {
unlink("./data", recursive = TRUE)
}
# OK - we should be in a base state setup test...
if (!require(xlsx)) {
install.packages("xlsx")
}
if (!file.exists("data")) {
dir.create("data")
}
# Download the file as a CSV file (Deliberate mistake) not a XLSX file
# This causes the error seen when read.xlsx is invoked...
# To fix replace rows.csv with rows.xlsx
if (!file.exists("data/cameras.xlsx")) {
fileUrl <- "https://data.baltimorecity.gov/api/views/dz54-2aru/rows.csv?accessType=DOWNLOAD"
download.file(fileUrl, destfile = "./data/cameras.xlsx", method = "curl")
}
list.files("./data")
# Now we check the file exists and read in the data...
# read.xlsx will throw the java error as the file downloaded is not a valid excel file...
if (!file.exists(".data/cameraData.xlsx")) {
cameraData.xlsx <- read.xlsx("./data/cameras.xlsx", sheetIndex=1, header = TRUE)
}
head(cameraData.xlsx)