使用xlsx包将数据从R插入到excel中的问题 [英] Issues using xlsx package to insert data from R to excel

查看:306
本文介绍了使用xlsx包将数据从R插入到excel中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从R创建一个新的excel工作簿,以使用xlsx包来保存一些小数据集。由于某种原因,它的工作正常,但我无法再做。



创建新工作簿的代码

 库(xlsx)
库(xlsxjars)
库(rJava)

文件< - marca_imei.xlsx
wb< - loadWorkbook(file)

#错误:
#.jcall中的错误(RJavaTools ,Ljava / lang / Object;,invokeMethod,cl,:
#java.lang.IllegalArgumentException:您的InputStream既不是OLE2流,也不是OOXML流

我已经搜索了一个答案,但似乎人们在从excel导入数据时遇到相同的错误
我试过了被推荐,但它没有工作,以下是未来搜索者的一些链接:





sessionInfo( )

  locale:
[1] LC_COLLATE = Spanish_Spain.1252 LC_CTYPE = Spanish_Spain .1252 LC_MONETARY = Spanish_Spain.1252
[4] LC_NUMERIC = C LC_TIME = Spanish_Spain.1252

附加的基本包:
[1]统计图形grDevices utils数据集方法base

其他附加包:
[1] xlsx_0.5.5 xlsxjars_0.6.0 RJDBC_0.2-3 rJava_0.9-6
[5] DBI_0.2-7 slidifyLibraries_0.3.1 slidify_0。 4 knitr_1.5
[9] devtools_1.4.1 scales_0.2.3 ggplot2_0.9.3.1 data.table_1.8.11
[13] reshape2_1.2.2

通过命名空间加载(而不附加):
[1] col orspace_1.2-4 dichromat_2.0-0 digest_0.6.4 evaluate_0.5.1 formatR_0.10
[6] grid_3.0.2 gtable_0.1.2 httr_0.2 labels_0.2 markdown_0.6.3
[11] MASS_7。 3-29 memoise_0.1 munsell_0.4.2 parallel_3.0.2 plyr_1.8
[16] proto_0.3-10 RColorBrewer_1.0-5 RCurl_1.95-4.1 stringr_0.6.2 tools_3.0.2
[21] whisker_0.3-2 yaml_2.1.10


解决方案

Martin, / p>

我相信问题是您正在阅读的文件不是有效的.xlsx文件。以下是重现您问题的代码示例。您也可以修改示例来解决问题。该示例使用Web中的示例数据集(Speed Camera locations baltimore :-))。



本质上,第16行是第26行触发错误的罪魁祸首,用于生成您看到的错误。

  .jcall中的错误(RJavaTools,Ljava / lang / Object;,invokeMethod,cl,
`java.lang.IllegalArgumentException:您的InputStream不是一个OLE2流,也不是OOXML流

重现错误下载文件rows.csv当您在第26行调用read.xlsx时,会触发您看到的错误。要修改第16行下载rows.xlsx并重新运行以下脚本:

 #!/ usr / bin / env Rscript 

#确保清理安装...
#卸载软件包
if(require(xlsx)) {
detach(package:xlsx,unload = TRUE)
}
if(require(xlsxjars)){
detach(package:xlsxjars,unload = TRUE)
}
#删除环境...
rm(list = ls())

#删除目录
if(file.exists(data )){
unlink(./ data,recursive = TRUE)
}

#确定 - 我们应该在基础状态设置测试...

如果(!require(xlsx)){
安装$($)


如果(!file.exists(data)){
dir.create(data)
}

#以CSV文件(故意错误)下载文件而不是XLSX文件
#这会导致在调用read.xlsx时看到的错误...
#要修复将rows.csv替换为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)

#现在我们检查文件是否存在并读取数据...
#read.xlsx将抛出java错误,因为下载的文件不是有效的excel文件...

if(!file.exists(。data / cameraData.xlsx)){
cameraData。 xlsx< - read.xlsx(./ data / cameras.xlsx,sheetIndex = 1, header = TRUE)
}

头(cameraData.xlsx)

以下是示例输出:


  1. 加载rows.csv ...


    source('test.R')
    加载所需的软件包:xlsx
    加载所需的软件包:xlsxjars
    %总收到%Xferd平均速度时间时间当前
    Dload上传总剩余速度
    0 0 0 0 0 0 0 0 - : - : - - : - : - - : - : - 0 0 0 0 0 0 0 0 0 - : - : - - : - : - - : - : - 0100 9294 100 9294 0 0 33870 0 - : - : - - - : - : - - : - : - 33796
    .jcall中的错误(RJavaTools,Ljava / lang / Object;,invokeMethod,cl,
    java .lang.IllegalArgumentException:您的InputStream既不是OLE2流,也不是OOXML流


    现在我们用rows.xlsx替换rows.csv ...


 > source('test.R',echo = TRUE)> #!/ usr / bin / env Rscript> > #确保清洁设置...> #卸载包> if(require(xlsx)){+ detach(package:xlsx,unload = TRUE)+}> if(require(xlsxjars)){+ detach(package:xlsxjars,unload = TRUE)+}> #删除环境...> rm(list = ls())> #删除目录> if(file.exists(data)){+ unlink(./ data,recursive = TRUE)+}> #OK  - 我们应该在基础状态设置测试...> >如果(!require(xlsx)){+ install.packages(xlsx)+}加载所需的包:xlsxLoading所需的包:xlsxjars> if(!file.exists(data)){+ dir.create(data)+}> #将文件下载为CSV文件(故意错误)而不是XLSX文件> #这会导致在调用read.xlsx时看到错误...> #修复副本.... [TRUNCATED]%总收到%Xferd平均速度时间时间时间当前Dload上传总剩余速度0 0 0 0 0 0 0 0  - : - : -   - : - : -   - : - : -  0100 9923 100 9923 0 0 48559 0  - : - : -   - : - : -   - : - : -  48642> list.files(./ data)[1]cameras.xlsx> #现在我们检查文件是否存在并读入数据...> #read.xlsx将抛出java错误,因为下载的文件不是有效的excel文件...> .... [TRUNCATED]>头(cameraData.xlsx)地址方向街道十字路口位置11 S CATON AVE& BENSON AVE N / B Caton Ave Benson Ave Caton Ave& Benson Ave(39.2693779962,-76.6688185297)2 S CATON AVE& BENSON AVE S / B Caton Ave Benson Ave Caton Ave& Benson Ave(39.2693157898,-76.6689698176)3 WILKENS AVE& PINE HEIGHTS AVE E / B Wilkens Ave Pine Heights Wilkens Ave& Pine Heights(39.2720252302,-76.676960806)4 THE ALAMEDA& E 33RD ST S / B阿拉米达33rd St The Alameda& 33rd St(39.3285013141,-76.5953545714)5 E 33RD ST& ALAMEDA E / B E 33rd Alameda E 33rd&阿拉米达(39.3283410623,-76.5953594625)6 ERDMAN AVE& N MACON ST E / B Erdman Macon St Erdman& Macon St(39.3068045671,-76.5593167803)>  


I´m trying to create a new excel workbook from R to save a few small datasets using xlsx package. For some reason it was working fine, but i´m unable to do it again.

Code to create a new workbook

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

I´ve searched for an answer but it seems people are having the same error when importing data from excel. I´ve tried what was recommended but it didn´t work. Here are some links for future searchers:

sessionInfo():

locale:
[1] LC_COLLATE=Spanish_Spain.1252  LC_CTYPE=Spanish_Spain.1252    LC_MONETARY=Spanish_Spain.1252
[4] LC_NUMERIC=C                   LC_TIME=Spanish_Spain.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] xlsx_0.5.5             xlsxjars_0.6.0         RJDBC_0.2-3            rJava_0.9-6           
 [5] DBI_0.2-7              slidifyLibraries_0.3.1 slidify_0.4            knitr_1.5             
 [9] devtools_1.4.1         scales_0.2.3           ggplot2_0.9.3.1        data.table_1.8.11     
[13] reshape2_1.2.2        

loaded via a namespace (and not attached):
 [1] colorspace_1.2-4   dichromat_2.0-0    digest_0.6.4       evaluate_0.5.1     formatR_0.10      
 [6] grid_3.0.2         gtable_0.1.2       httr_0.2           labeling_0.2       markdown_0.6.3    
[11] MASS_7.3-29        memoise_0.1        munsell_0.4.2      parallel_3.0.2     plyr_1.8          
[16] proto_0.3-10       RColorBrewer_1.0-5 RCurl_1.95-4.1     stringr_0.6.2      tools_3.0.2       
[21] whisker_0.3-2      yaml_2.1.10     

解决方案

Martin,

I believe the issue is that the file you are reading in is not a valid .xlsx file. Here is a code example to reproduce your problem. You can also modify the example to solve the problem. The example uses an example data set from the web (Speed Camera locations baltimore :-)).

In essence line 16 is the culprit of the error triggered on line 26 that generates the error you see.

Error in .jcall("RJavaTools", "Ljava/lang/Object;", "invokeMethod", cl,  : 
`java.lang.IllegalArgumentException: Your InputStream was neither an OLE2 stream, nor an OOXML stream

to reproduce the error download the file "rows.csv", when you invoke read.xlsx on line 26 it triggers the error you see. To fix change line 16 to download "rows.xlsx" and rerun the script below:

#!/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)

Here is the example output:

  1. Load rows.csv...

    source('test.R') Loading required package: xlsx Loading required package: xlsxjars % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0100 9294 100 9294 0 0 33870 0 --:--:-- --:--:-- --:--:-- 33796 Error in .jcall("RJavaTools", "Ljava/lang/Object;", "invokeMethod", cl, : java.lang.IllegalArgumentException: Your InputStream was neither an OLE2 stream, nor an OOXML stream

    now we replace rows.csv with rows.xlsx...

> source('test.R', echo=TRUE)

> #!/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")
+ }
Loading required package: xlsx
Loading required package: xlsxjars

> 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 replac .... [TRUNCATED] 
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0100  9923  100  9923    0     0  48559      0 --:--:-- --:--:-- --:--:-- 48642

> list.files("./data")
[1] "cameras.xlsx"

> # 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...
> .... [TRUNCATED] 

> head(cameraData.xlsx)
                         address direction      street  crossStreet               intersection                      Location.1
1       S CATON AVE & BENSON AVE       N/B   Caton Ave   Benson Ave     Caton Ave & Benson Ave (39.2693779962, -76.6688185297)
2       S CATON AVE & BENSON AVE       S/B   Caton Ave   Benson Ave     Caton Ave & Benson Ave (39.2693157898, -76.6689698176)
3 WILKENS AVE & PINE HEIGHTS AVE       E/B Wilkens Ave Pine Heights Wilkens Ave & Pine Heights  (39.2720252302, -76.676960806)
4        THE ALAMEDA & E 33RD ST       S/B The Alameda      33rd St     The Alameda  & 33rd St (39.3285013141, -76.5953545714)
5        E 33RD ST & THE ALAMEDA       E/B      E 33rd  The Alameda      E 33rd  & The Alameda (39.3283410623, -76.5953594625)
6        ERDMAN AVE & N MACON ST       E/B      Erdman     Macon St         Erdman  & Macon St (39.3068045671, -76.5593167803)
> 

这篇关于使用xlsx包将数据从R插入到excel中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆