2013-11-14 64 views
6

我有以下請求如何捕獲RCurl詳細輸出

library(RCurl) 
res=getURL("http://www.google.com/search?hl=en&lr=&ie=ISO-8859-1&q=RCurl&btnG=Search", 
      .opts=list(verbose = TRUE) 
      ) 

和想捕捉呼叫的詳細輸出(即,什麼被印刷在紅色R控制檯)。我認爲輸出行是消息,因此打印到stderr()。以下作品的消息

sink(textConnection("test","w"),type="message") 
message("test message") 
sink(stderr(),type="message") 
test 
#[1] "test message" 

但如果我被要求RCurl更換res=getURL(.....)作爲message("test message")上面給出。 顯然,RCurl的輸出不會打印到stderr()。它也不打印到stdout()

那麼,如何捕獲輸出?

紅利問題:sink(stderr(),type="message")是否將連接設置回R的默認值?

謝謝你的幫助!

+1

發送輸出回控制檯:'sink()'輸出和'sink(type =「me ssage「)'stderr。在'?中看到例子? sink'。 – Thomas

+0

好的!所以我可以省略'stderr()'。謝謝! – cryo111

回答

8

您需要使用debugGatherer功能:使用

d <- debugGatherer() 
x <- getURL("http://www.google.com/search?hl=en&lr=&ie=ISO-8859-1&q=RCurl&btnG=Search", 
    debugfunction = d$update, verbose = TRUE) 

然後,您可以拉出verbose內容:

d$value() 

但是,我想你只想以下兩個要素:

> cat(d$value()['text']) 
About to connect() to www.google.com port 80 (#0) 
    Trying 173.194.112.176... connected 
Connected to www.google.com (173.194.112.176) port 80 (#0) 
Connection #0 to host www.google.com left intact 
Closing connection #0 

> cat(d$value()['headerIn']) 
HTTP/1.1 200 OK 

Date: Thu, 14 Nov 2013 19:54:18 GMT 

Expires: -1 

Cache-Control: private, max-age=0 

Content-Type: text/html; charset=ISO-8859-1 

Set-Cookie: PREF=ID=783ad15e124023b0:FF=0:TM=1384458858:LM=1384458858:S=GuYBk1a3SfTJBIjh; expires=Sat, 14-Nov-2015 19:54:18 GMT; path=/; domain=.google.com 

Set-Cookie: NID=67=sNsGhMCgjGZFtILEodYKCjxsi0Yio3oSA4xHakDGVHQKxG-fJlY05AlYlJf4Wwcto2HY2uP5Zt2iWxA4Dt0KUWxq14J-F-KvJ38zoBhWBWNxm6Ju0Oupl8gj41USR0PB; expires=Fri, 16-May-2014 19:54:18 GMT; path=/; domain=.google.com; HttpOnly 

P3P: CP="This is not a P3P policy! See http://www.google.com/support/accounts/bin/answer.py?hl=en&answer=151657 for more info." 

Server: gws 

X-XSS-Protection: 1; mode=block 

X-Frame-Options: SAMEORIGIN 

Transfer-Encoding: chunked 
+0

謝謝,托馬斯。解決方案效果非常好! – cryo111