2014-10-18 46 views
0

我使用R的download.file(..., method="curl")來下載各種文本文件。從捲曲的狀態更新不必每次更新後「\ n」,所以出來的東西看起來是這樣的,沒有換行:使用設置捲曲選項以提高R的可讀性progress.file()

> url1 <- "https://d396qusza40orc.cloudfront.net/getdata%2Fdata%2Fss06hid.csv" 
> q1f <- "wk3q1f.csv" 
> download.file(url1,q1f,method="curl") 
    % Total % Received % Xferd Average Speed Time Time  Time Current 
           Dload Upload Total Spent Left Speed 
    0  0 0  0 0  0  0  0 --:--:-- --:--:-- --:--:--  0 0  0 0  0 0  0  0  0 --:--:-- --:--:-- --:--:--  0 0 4147k 0 18404 0  0 14232  0 0:04:58 0:00:01 0:04:57 14233 2 4147k 2 114k 0  0 51344  0 0:01:22 0:00:02 0:01:20 51341 

版本:libcurl中7.30.0,R爲3.1.0 OS X.

是否有捲曲的選擇,我可以爲線設置的中斷,使像這樣的進度報告:

% Total % Received % Xferd Average Speed Time Time  Time Current 
           Dload Upload Total Spent Left Speed 
    0  0 0  0 0  0  0  0 --:--:-- --:--:-- --:--:--  0 
    0  0 0  0 0  0  0  0 --:--:-- --:--:-- --:--:--  0 
    0 4147k 0 18404 0  0 14232  0 0:04:58 0:00:01 0:04:57 14233 
    2 4147k 2 114k 0  0 51344  0 0:01:22 0:00:02 0:01:20 51341 

我看着嫋嫋-config並沒有看到任何東西。

回答

2

沒有選擇使curl使用\n而不是我所知道的\r。不過,你可以自己做。這是OS X特定的答案,但可以適用於Linux。使用homebrew做一個brew install coreutils,所以我們可以訪問gstdbuf這將幫助我們得到無緩衝的命令輸出。

接下來,寫一個小的shell腳本一條線(我把它叫做mycurl):

gstdbuf -i0 -o0 -e0 curl $1 -o $2 2>&1 | gstdbuf -i0 -o0 -e0 tr '\r' '\n' 

確保它的可執行文件(chmod 755 mycurl

download.file少了點以下,如果method="curl"

else if (method == "curl") { 
    if (quiet) 
     extra <- c(extra, "-s -S") 
    if (!cacheOK) 
     extra <- c(extra, "-H 'Pragma: no-cache'") 
    status <- system(paste("curl", paste(extra, collapse = " "), 
     shQuote(url), " -o", shQuote(path.expand(destfile)))) 

所以,我們可以用它來模仿:

status <- system(paste("/path/to/mycurl", shQuote(url1), shQuote(path.expand(q1f)))) 

這將爲您提供換行符的下載進度。

Linux用戶可以使用JUSE VS stdbufgstdbuf由於coreutileshomebrew包預先考慮到g的命令。

或者,你可以使用GEThttr包帶write_disk選項,並使用更R類似的進度表:

library(httr) 

status <- GET(url1, write_disk(path.expand(q1f), overwrite=TRUE), progress("down")) 
|==================================================== (etc. to 100%)| 
+0

很不錯的。我從這個答案中學到了很多東西。謝謝! – C8H10N4O2 2014-10-18 13:47:59