Downloading large files with R/RCurl efficiently

前端 未结 2 613
陌清茗
陌清茗 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 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")
    

提交回复
热议问题