2017-03-08 50 views
1

下面是一个例子:如何捕获系统2()输出中的R

RR<-"/usr/bin/R" 
x<-system2(RR, "--version") 
R version 3.3.3 (2017-03-06) -- "Another Canoe" 
Copyright (C) 2017 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 the terms of the 
GNU General Public License versions 2 or 3. 
For more information about these matters see 
http://www.gnu.org/licenses/. 
> x 
[1] 0 

正如你可以在这里看到的x is 0。我的问题是如何将'system2(RR,“--version”)'输出分配给x

回答

2

这在?system2的文档中有描述。如果你想捕捉你的程序的输出作为一个字符向量,集stdout=TRUE

x <- system2(RR, "--version", stdout=TRUE) 

“标准输出”是节目的“标准输出”。这是程序用来识别程序“结果”的常用术语。

+0

大,对你的帮助谢谢! –

+0

我做的差不多相同[这里](https://stackoverflow.com/questions/45426902/system2-to-call-python2-and-python3-inside-r)但我没有得到预期的输出,想法? – hhh

0

使用system代替:

x <- system(paste(RR,'--version'), intern=TRUE) 
+0

我认为这是为system()而不是system2()? –

+0

你是对的,我编辑了我的答案。 – user890739