RCurl:在Rgui显示进度表

时间:2014-02-12 15:03:01

标签: r curl rcurl

使用R.exeRterm.exe,这可以提供出色的进度表。

page=getURL(url="ftp.wcc.nrcs.usda.gov", noprogress=FALSE) 

在Rgui,我受限于:

page=getURL(url="ftp.wcc.nrcs.usda.gov", 
            noprogress=FALSE, progressfunction=function(down,up) print(down))

提供了非常有限的下载信息集。

有没有办法改善这个?

3 个答案:

答案 0 :(得分:3)

  

我开始怀疑使用标准R命令可以重新打印覆盖当前行,这是RCurl在非GUI模式下所做的。

我很高兴告诉我错了。至少对于一行,\r可以做到这一点。事实上:

conc=function(){
    cat(" abcd")
    cat(" ABCD", '\n')

}
conc()

# abcd ABCD 

可是:

over=function(){
    cat(" abcd")
    cat("\r ABCD", "\n")
}
over()

# ABCD

这个,我写了这个progressDown函数,它可以在同一行上监视下载状态重写:

library(RCurl) # Don't forget

### Callback function for curlPerform
progressDown=function(down, up, pcur, width){
    total=as.numeric(down[1]) # Total size as passed from curlPerform
    cur=as.numeric(down[2])   # Current size as passed from curlPerform
    x=cur/total
    px= round(100 * x)
    ## if(!is.nan(x) &&  px>60) return(pcur) # Just to debug at 60%
    if(!is.nan(x) && px!=pcur){
        x= round(width * x)
        sc=rev(which(total> c(1024^0, 1024^1, 1024^2, 1024^3)))[1]-1
        lb=c('B', 'KB', 'MB', 'GB')[sc+1]
        cat(paste(c(
            "\r  |", rep.int(".", x), rep.int(" ", width - x),
            sprintf("| %g%s of %g%s %3d%%",round(cur/1024^sc, 2), lb, round(total/1024^sc, 2), lb, px)),
                  collapse = ""))
        flush.console() # if the outptut is buffered, it will go immediately to console
        return(px)
    }
    return(pcur)
}

现在我们可以使用curlPerform

的回调
curlProgress=function(url, fname){
    f = CFILE(fname, mode="wb")
    width= getOption("width") - 25   # you can make here your line shorter/longer
    pcur=0
    ret=curlPerform(url=url, writedata=f@ref,  noprogress=FALSE,
        progressfunction=function(down,up) pcur<<-progressDown(down, up, pcur, width),
        followlocation=T)
        close(f)
        cat('\n Download', names(ret), '- Ret', ret, '\n') # is success? 
}

使用小样本二进制文件运行它:

curlProgress("http://www.nirsoft.net/utils/websitesniffer-x64.zip", "test.zip")

60%的中间输出是(无#保护):

  |.................................                      | 133.74KB of 222.75KB  60%

其中KB将根据总大小调整为B, KB, MB, GB

成功状态的最终输出是:

  |.......................................................| 222.61KB of 222.75KB 100%
 Download OK - Ret 0 

注意,输出行宽度是相对于R width选项(控制行上的最大列数),可以自定义更改curlProgress行:

width= getOption("width") - 25

这足以满足我的需求,并解决了我自己的问题。

答案 1 :(得分:2)

以下是使用txtProgressBar的简单示例。基本上,只需先执行HEAD请求即可获取要检索的文件的文件大小,然后设置txtProgressBar作为其最大大小。然后,您使用progressfunction curlPerform参数来调用setTxtProgressBar。这一切都很好用(除非没有“内容长度”标题,在这种情况下,此代码只是不打印进度条)。

url <- 'http://stackoverflow.com/questions/21731548/rcurl-display-progress-meter-in-rgui'

h <- basicTextGatherer()
curlPerform(url=url, customrequest='HEAD',
            header=1L, nobody=1L, headerfunction=h$update)

if(grepl('Transfer-Encoding: chunked', h$value())) {
    size <- 1
} else {
    size <- as.numeric(strsplit(strsplit(h$value(),'\r\nContent-Type')[[1]][1],
                                                   'Content-Length: ')[[1]][2])
}

bar <- txtProgressBar(0, size)
h2 <- basicTextGatherer()
get <- curlPerform(url=url, noprogress=0L,
                   writefunction=h2$update, 
                   progressfunction=function(down,up)
                       setTxtProgressBar(bar, down[2]))

h2$value() # return contents of page

控制台上的输出只是一堆======

答案 2 :(得分:1)

怎么样:

curlProgress=function(url, fname){
    f = CFILE(fname, mode="wb")
    prev=0
    ret=curlPerform(url=url, writedata=f@ref,  noprogress=FALSE,
        progressfunction=function(a,b){
            x=round(100*as.numeric(a[2])/as.numeric(a[1]))
            if(!is.nan(x) && x!=prev &&round(x/10)==x/10) prev<<-x else x='.'
            cat(x)      
        }, followlocation=T)
    close(f)
    cat(' Download', names(ret), '- Ret', ret, '\n')
}


它打印点数或百分比下载可被10整除,并打破50%的线 并使用一个小的223 KB文件:

curlProgress("http://www.nirsoft.net/utils/websitesniffer-x64.zip", "test.zip")

听起来像这样:

................10...............20................30...............40...............50 
..............................70...............80...............90...............100... Download OK - Ret 0 

我开始怀疑使用标准R命令可以重新打印覆盖当前行,这是RCurl在非GUI模式下所做的。

相关问题