cry*_*111 6 r stdout stderr rcurl sink
我有以下要求
library(RCurl)
res=getURL("http://www.google.com/search?hl=en&lr=&ie=ISO-8859-1&q=RCurl&btnG=Search",
.opts=list(verbose = TRUE)
)
Run Code Online (Sandbox Code Playgroud)
并且想要捕获调用的详细输出(即,在R控制台中以红色打印的内容).我认为输出行是消息,因此打印到stderr().以下适用于消息
sink(textConnection("test","w"),type="message")
message("test message")
sink(stderr(),type="message")
test
#[1] "test message"
Run Code Online (Sandbox Code Playgroud)
但如果我用上面给出message("test message")的RCurl请求替换,则不会res=getURL(.....).显然,RCurl的输出没有打印到stderr().它也没有打印到stdout().
那么,我如何捕获输出?
加分问题:sink(stderr(),type="message")将连接设置回R的默认值的正确方法是什么?
谢谢您的帮助!
你需要使用这个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)
Run Code Online (Sandbox Code Playgroud)
然后,您可以verbose使用以下内容提取内容:
d$value()
Run Code Online (Sandbox Code Playgroud)
但是,我想你只想要以下两个元素:
> 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
Run Code Online (Sandbox Code Playgroud)