How to open an .xlsb file in R?

前端 未结 4 1924
自闭症患者
自闭症患者 2020-12-14 20:13

I\'m trying to open an .xlsb file in R and keep getting similar errors.

Any recommendations on how to solve this issue without having to download the data and save

4条回答
  •  自闭症患者
    2020-12-14 20:50

    One way could be to use ODBC:

    require(RODBC)
    if (any(grepl("*.xlsb", odbcDataSources(), fixed = TRUE))) {
      download.file(url = "http://phx.corporate-ir.net/External.File?item=UGFyZW50SUQ9NTcwMjI1fENoaWxkSUQ9MjcxMjIxfFR5cGU9MQ==&t=1", 
                    destfile = file.path(tempdir(), "test.xlsb"), 
                    mode = "wb")
      conn <- odbcConnectExcel2007( file.path(tempdir(), "test.xlsb")) 
      df <- sqlFetch(conn, sub("'(.*)\\$'", "\\1", sqlTables(conn)$TABLE_NAME)[4]) # read 4th sheet in the table name list
      head(df, 10)
      #                                             F1          F2         F3       F4        F5 F6
      # 1                                                                    NA
      # 2                                                                    NA
      # 3                                                                    NA
      # 4                                                                    NA
      # 5  Baker Hughes Gulf of Mexico Oil / Gas Split                           NA
      # 6                                                                    NA
      # 7                                          US Offshore Total\nGoM Gas\nGoM Oil \nGoM NA
      # 8                                       1/7/00         127        123      116         7 NA
      # 9                                      1/14/00         125        121      116         5 NA
      # 10                                     1/21/00         125        121      116         5 NA
      close(conn) 
    }
    

提交回复
热议问题