Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
441 views
in Technique[技术] by (71.8m points)

r - How to capture the output of system()

This question was motivated by Rmarkdown not outputting results of system command to html file. For some reason, the output of system() in R (or system2()) cannot be captured by sink() or capture.output(), so currently there is no way for knitr to record the output. For example, in the R console:

> system('ls')
DESCRIPTION
NAMESPACE
R
README.md
inst
man

but in a knitr document, you won't see the output, because capture.output(system('ls')) is character(0), i.e. the output cannot be captured. Of course I can do cat(system('ls', intern = TRUE), sep = ' ') as I mentioned in the answer of that question, but this is kind of awkward. I wonder if it is a way to capture the output of system() without using intern = TRUE and cat().


Update: see https://github.com/yihui/knitr/issues/1203 for a hack that I provided to solve the problem.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I don't think you can do this (at least on *nix systems; I don't have a Windows/Mac system handy) because system appears to invisibly return the value returned by the command executed, and R doesn't appear to redirect the command's output to the R console.

This is because the stdout of your terminal is not the same as the R console "stdout". What you see from within your R session is the mix of the terminal's stdout and the R process output. capture.output is looking for the R process' output, not all output to stdout from the parent process.

You can start a process that prints to stdout, put it in the background, then start R... and you'll see that process' output in your "R output", similar to if you had run system("ping -c5 8.8.8.8") from R.

josh@computer: /home/josh
> ping -c5 8.8.8.8 & R
[1] 5808
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=46 time=39.9 ms

R version 3.2.4 Revised (2016-03-16 r70336) -- "Very Secure Dishes"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> 64 bytes from 8.8.8.8: icmp_seq=2 ttl=46 time=38.1 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=46 time=38.3 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=46 time=38.4 ms
64 bytes from 8.8.8.8: icmp_seq=5 ttl=46 time=38.3 ms

--- 8.8.8.8 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4003ms
rtt min/avg/max/mdev = 38.176/38.656/39.986/0.703 ms

> q()
Save workspace image? [y/n/c]: n
[1]+  Done                    ping -c5 8.8.8.8

josh@computer: /home/josh
> 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...