Issues using xlsx package to insert data from R to excel

前端 未结 2 1323
野趣味
野趣味 2021-01-12 20:57

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.

相关标签:
2条回答
  • 2021-01-12 21:44

    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)
    > 

    0 讨论(0)
  • 2021-01-12 21:48

    It is possible the problem is with Java, not XLConnect. Be sure you have Java installed by taking the test on the Java site -- it will confirm Java is correctly installed. Then make sure R knows the path to find the jre.dll or something like that file name for what is crucial.

    Second, here is the code I have been using for a year, without the error message you got. If it helps you ....

    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
    }
    
    0 讨论(0)
提交回复
热议问题