开发者

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

开发者 https://www.devze.com 2023-02-05 11:28 出处:网络
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 t

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

精彩评论

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

关注公众号