RCurl:在Rgui中显示进度表 [英] RCurl: Display progress meter in Rgui
问题描述
使用 R.exe
或 Rterm.exe
,这将是一个很好的进度表。
Using R.exe
or Rterm.exe
, this gives an excellent progress meter.
page=getURL(url="ftp.wcc.nrcs.usda.gov", noprogress=FALSE)
在Rgui中,我限制为:
In Rgui I am limited to:
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.
有办法改善吗?
推荐答案
我开始怀疑使用标准R命令可以重写覆盖当前行,这是RCurl在非GUI模式下所做的。
I start doubting that with standard R commands it is possible to reprint overwriting the current line, which is what RCurl does in non-GUI mode.
我很高兴告诉我我错了。至少对于单行, \r
可以做的伎俩。事实上:
I am glad to tell that I was wrong. At least for a single line, \r
can do the trick. In fact:
conc=function(){
cat(" abcd")
cat(" ABCD", '\n')
}
conc()
# abcd ABCD
但是:
over=function(){
cat(" abcd")
cat("\r ABCD", "\n")
}
over()
# ABCD
这样,我写了 progressDown
可以监视下载状态重写始终在同一行:
That given, I wrote this progressDown
function, which can monitor download status rewriting always on the same 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)
}
现在我们可以使用 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?
}
使用小型示例二进制文件运行:
Running it with a small sample binary:
curlProgress("http://www.nirsoft.net/utils/websitesniffer-x64.zip", "test.zip")
中间输出为60%是(无#
保护) / p>
the intermediate output at 60% is (no #
protection):
|................................. | 133.74KB of 222.75KB 60%
其中 KB
,将根据总大小调整为<code> B,KB,MB,GB 。
where KB
, will be adjusted to B, KB, MB, GB
, based on total size.
具有成功状态的最终产品是:
Final output with success status, is:
|.......................................................| 222.61KB of 222.75KB 100%
Download OK - Ret 0
width是相对于R width选项(它控制一行上的最大列数),并且可以自定义改变 curlProgress
行:
Note, the output line width is relative to R width option (which controls the maximum number of columns on a line) and can be customised changing the curlProgress
line:
width= getOption("width") - 25
$ b b
这足以满足我的需要,并解决我自己的问题。
This is enough for my needs and solves my own question.
这篇关于RCurl:在Rgui中显示进度表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!