[英]Add progress meter to header in Shiny (shinydashboard/shinydashboardplus)
[英]RCurl: Display progress meter in Rgui
使用R.exe
或Rterm.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))
它提供了非常有限的下載信息集。
有沒有辦法改善這個?
我開始懷疑使用標准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
這足以滿足我的需求並解決了我自己的問題。
這是一個使用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
輸出只是控制台上的一堆======
。
關於什么:
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模式下所做的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.