我開始懷疑,使用標準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, [email protected], 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
line:
width= getOption("width") - 25
這足夠滿足我的需求並解決我自己的問題。