RCurl: display progress bar in Rgui

Using R.exeor Rterm.exe, this gives an excellent progress bar.

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

In Rgui, I am limited:

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

which gives a very limited set of download information.

Is there any way to improve this?

+3
source share
3 answers

I'm starting to doubt that with standard R commands you can rewrite the rewriting of the current line, which RCURL does in non-GUI mode.

I am pleased to report that I was mistaken. At least one line \rcan do the trick. Actually:

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

}
conc()

# abcd ABCD 

But:

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

# ABCD

This is given, I wrote this function progressDown, which can always track the reload status of the boot on the same line:

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)
}

Now we can use the callback with 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 ( ) curlProgress:

width= getOption("width") - 25

.

+3

, 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

:

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 :

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.

+1

All Articles