2016-08-05 3 views
0

私はPythonスクリプトからrpy2を使って呼び出すR関数を持っています。 R関数はstdoutに出力します。 rスクリプトが呼び出されたときにstdoutを消すパラメータがrpy2にありますか?rpy2でstdoutへの出力を無音にするにはどうしたらいいですか?

+0

'StringIO'を使って' stdout'の書き込みを止めることができます。 Python 2では 'StringIo'モジュールに、Python 3では' io'モジュールにあります。 – cdarke

+0

R側から、 'invisible(expression)'を使って印刷方法を隠すことができます – Shape

答えて

0

rpy2では、端末とのR自身のやりとりをPython関数として再定義できます。

ドキュメントから次の例は、(物事の壮大な計画では限られた有用性が、それは機能の簡単な例を作る)Pythonのリストにstdoutに出力 を追加する方法を示しています。

buf = [] 
def f(x): 
    # function that append its argument to the list 'buf' 
    buf.append(x) 

# output from the R console will now be appended to the list 'buf' 
rinterface.set_writeconsole(f) 

date = rinterface.baseenv['date'] 
rprint = rinterface.baseenv['print'] 
rprint(date()) 

# the output is in our list (as defined in the function f above) 
print(buf) 


# restore default function 
rinterface.set_writeconsole(rinterface.consolePrint) 

ドキュメントは以下にあります:http://rpy2.readthedocs.io/en/version_2.8.x/callbacks.html#console-i-o

関連する問題