Read Excel file into R with XLConnect package from URL

前端 未结 4 1651
轮回少年
轮回少年 2020-12-19 21:32

There are lots of good examples out there on how to read Microsoft Excel files into R with the XLConnect package, but I can\'t find any examples of how to read in an Excel f

相关标签:
4条回答
  • 2020-12-19 22:12

    Two things:

    1. Try using a different package--I know the gdata package's read.xls function has support for URLs

    2. Try loading in a publicly-available xls file to make sure it's not an issue with the particular website.

    For instance, you can try:

    library("gdata")
    site <- "http://www.econ.yale.edu/~shiller/data/chapt26.xls"
    data  <- read.xls(site, header=FALSE, skip=8)
    head(data)
    
    0 讨论(0)
  • 2020-12-19 22:16

    XLConnect does not support importing directly from URLs. You have to use e.g. download.file first to download the file to your local machine:

    require(XLConnect)
    tmp = tempfile(fileext = ".xls")
    download.file(url = "http://www.econ.yale.edu/~shiller/data/chapt26.xls", destfile = tmp)
    readWorksheetFromFile(file = tmp, sheet = "Data", header = FALSE, startRow = 9, endRow = 151)
    

    or with your originally proposed URL:

    require(XLConnect)
    tmp = tempfile(fileext = ".xls")
    download.file(url = "https://www.misoenergy.org/Library/Repository/Market%20Reports/20140610_sr_nd_is.xls", destfile = tmp, method = "curl")
    readWorksheetFromFile(file = tmp, sheet = "Sheet1", header = TRUE, startRow = 11, startCol = 2, endCol = 13)
    
    0 讨论(0)
  • 2020-12-19 22:17
    library(relenium)
    library(XML)
    library(RCurl)
    
    firefox=firefoxClass$new()
    url="https://www.misoenergy.org/Library/Repository/Market%20Reports/20140610_sr_nd_is.xls"
    url=sprintf(url)
    firefox$get(url)
    

    This will open a Firefox instance within R and ask you to download the file, which you could then open in the next line of code. I don't know of any R utilities that will open an excel spreadsheet from HTTPS.

    You could then set a delay while you're saving the file and then read the sheet from your downloads folder:

    Sys.sleep(10)
    sheet <- c("Sheet1")
    data  <- readWorksheetFromFile(path, sheet, header=TRUE, startRow=11, startCol=2, endCol=13)
    
    0 讨论(0)
  • 2020-12-19 22:20

    You can use RCurl to download the file:

    library(RCurl)
    library(XLConnect)
    appURL <- "https://www.misoenergy.org/Library/Repository/Market%20Reports/20140610_sr_nd_is.xls"
    f = CFILE("exfile.xls", mode="wb")
    curlPerform(url = appURL, writedata = f@ref, ssl.verifypeer = FALSE)
    close(f)
    out <- readWorksheetFromFile(file = "exfile.xls", sheet = "Sheet1", header = TRUE
                          , startRow = 11, startCol = 2, endCol = 15, endRow = 35)
    > head(out)
    Col1 EEI Col3 IESO MHEB Col6 PJM SOCO SWPP TVA WAUE Col12 Other Total
    1 Hour  1 272   NA  768 1671   NA 148  200  -52 198  280    NA   700  4185
    2 Hour  2 272   NA  769 1743   NA 598  200  -29 190  267    NA   706  4716
    3 Hour  3 272   NA  769 1752   NA 598  200  -28 194  267    NA   710  4734
    4 Hour  4 272   NA  769 1740   NA 598  200  -26 189  266    NA   714  4722
    5 Hour  5 272   NA  769 1753   NA 554  200  -27 189  270    NA   713  4693
    6 Hour  6 602   NA  769 1682   NA 218  200  -32 223  286    NA   714  4662
    
    0 讨论(0)
提交回复
热议问题