开发者

How can I suppress the output to console when importing packages in RPy2 in Python?

Whenever I run a script importing packages with import in RPy2 in Python, there are always some extra lines popping up in the con开发者_JAVA百科sole. I pasted in an example below. How can I suppress that behavior?

CookieJar:r cookies$ python script.py 

    ‘tseries’ version: 0.10-24

    ‘tseries’ is a package for time series analysis and computational
    finance.

    See ‘library(help="tseries")’ for details.


Besides require(tseries, quietly = TRUE) and using sink(), or its Python equivalent, there is also the simple

suppressMessages( library( tseries ))

which I prefer.


You could temporarily redirect the output stream to a blackhole just before the spammy peice of code.

import sys

class Blackhole(object):

    def write(self, string):
        pass

stdout = sys.stdout
sys.stdout = Blackhole()

function_el_spammo()

sys.stdout = stdout


In your R script, I would preload the tseries package (just in case if its called by some other functio/package) using

require(tseries, quietly = TRUE)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜