Downloading large files with R/RCurl efficiently

前端 未结 2 597
陌清茗
陌清茗 2020-12-28 08:24

I see that many examples for downloading binary files with RCurl are like such:

library(\"RCurl\")
curl = getCurlHandle()
bfile=getBinaryURL (
        \"http         


        
相关标签:
2条回答
  • 2020-12-28 08:48

    um.. use mode = 'wb' :) ..run this and follow along w/ my comments.

    # create a temporary file and a temporary directory on your local disk
    tf <- tempfile()
    td <- tempdir()
    
    # run the download file function, download as binary..  save the result to the temporary file
    download.file(
        "http://sourceforge.net/projects/peazip/files/4.8/peazip_portable-4.8.WINDOWS.zip/download",
        tf ,
        mode = 'wb' 
    )
    
    # unzip the files to the temporary directory
    files <- unzip( tf , exdir = td )
    
    # here are your files
    files
    
    0 讨论(0)
  • 2020-12-28 09:02

    This is the working example:

    library(RCurl)
    #
    f = CFILE("bfile.zip", mode="wb")
    curlPerform(url = "http://www.example.com/bfile.zip", writedata = f@ref)
    close(f)
    

    It will download straight to file. The returned value will be (instead of the downloaded data) the status of the request (0, if no errors occur).

    Mention to CFILE is a bit terse on RCurl manual. Hopefully in the future it will include more details/examples.

    For your convenience the same code is packaged as a function (and with a progress bar):

    bdown=function(url, file){
        library('RCurl')
        f = CFILE(file, mode="wb")
        a = curlPerform(url = url, writedata = f@ref, noprogress=FALSE)
        close(f)
        return(a)
    }
    
    ## ...and now just give remote and local paths     
    ret = bdown("http://www.example.com/bfile.zip", "path/to/bfile.zip")
    
    0 讨论(0)
提交回复
热议问题