コード例 #1
0
ファイル: test_EmbeddedR.py プロジェクト: jjhoyt/gengis
 def testSetFlushConsole(self):
     flush = {'count': 0}
     def f():
         flush['count'] = flush['count'] + 1
         
     rinterface.setFlushConsole(f)
     self.assertEquals(rinterface.getFlushConsole(), f)
     rinterface.baseNameSpaceEnv.get("flush.console")()
     self.assertEquals(1, flush['count'])
     rinterface.setWriteConsole(rinterface.consoleFlush)
コード例 #2
0
ファイル: test_EmbeddedR.py プロジェクト: jjhoyt/gengis
    def testSetWriteConsole(self):
        buf = []
        def f(x):
            buf.append(x)

        rinterface.setWriteConsole(f)
        self.assertEquals(rinterface.getWriteConsole(), f)
        code = rinterface.SexpVector(["3", ], rinterface.STRSXP)
        rinterface.baseNameSpaceEnv["print"](code)
        self.assertEquals('[1] "3"\n', str.join('', buf))
コード例 #3
0
ファイル: test_EmbeddedR.py プロジェクト: jjhoyt/gengis
    def testWriteConsoleWithError(self):
        if sys.version_info[0] == 2 and sys.version_info[1] < 6:
            self.assertTrue(False) # cannot be tested with Python < 2.6
            return None
        def f(x):
            raise Exception("Doesn't work.")
        rinterface.setWriteConsole(f)

        outfile = tempfile.NamedTemporaryFile(mode = 'w', 
                                              delete=False)
        stderr = sys.stderr
        sys.stderr = outfile
        try:
            code = rinterface.SexpVector(["3", ], rinterface.STRSXP)
            rinterface.baseNameSpaceEnv["print"](code)
        except Exception, e:
            sys.stderr = stderr
            raise e
コード例 #4
0
ファイル: CecogAnalyzer.py プロジェクト: cmci/cecog
    def test_r_import(self):
        try:
            import rpy2.robjects as robjects
            import rpy2.rinterface as rinterface
            import rpy2.robjects.numpy2ri

            # some tests
            x = robjects.r['pi']
            v = robjects.FloatVector([1.1, 2.2, 3.3, 4.4, 5.5, 6.6])
            m = robjects.r['matrix'](v, nrow=2)
            has_R_version = True
            version = '%s.%s' % (robjects.r['version'][5][0],
                                 robjects.r['version'][6][0])
        except:
            has_R_version = False
            msg = 'R installation not found.\n\n'\
                  'To use HMM error correction or plotting functions '\
                  'R >= Version 2.9 must be installed together with these.'\
                  'packages:\n'
            msg += ', '.join(R_LIBRARIES)
            msg += '\n\nSee http://www.r-project.org\n\n'
            critical(self, 'R installation not found', info=msg, detail_tb=True)


        if has_R_version:
            missing_libs = []
            buffer = []
            rinterface.setWriteConsole(lambda x: buffer.append(x))
            for lib_name in R_LIBRARIES:
                try:
                    robjects.r['library'](lib_name)
                except:
                    missing_libs.append(lib_name)
            rinterface.setWriteConsole(None)
            if len(missing_libs) > 0:
                msg = 'Missing R package(s)\n\n'
                msg += ', '.join(missing_libs)
                msg += '\n\nSee http://www.r-project.org\n\n'
                msg += '\n'.join(buffer)

                critical(self, 'Missing R libraries', info=msg, detail_tb=True)
                qApp.valid_R_version = False
            else:
                qApp.valid_R_version = True
コード例 #5
0
ファイル: rpy_exec_test.py プロジェクト: takluyver/ARE
        m = e.args[0].partition("eval(expr, envir, enclos) : ")[2]
        if m:
            raise TranslatedRRuntimeError("Error: " + m)
        else:
            raise TranslatedRRuntimeError(*e.args)
    visible = visible[0]
    if visible:
        return result

# There are various possibilities:
# * An error can be raised in parsing or evaling. We try to translate back
#     to the R error message, and show it.
# * The command may have a visible return value, e.g. "5 + 4", or not,
#     e.g. "a <- 1"
# * The command may cause output to the console, e.g. "print(1:4)".
consolebuffer = []
setWriteConsole(consolebuffer.append)
def Rconsoleexec(command):
    try:
        res = Rexec(command)
    except RRuntimeError as e:
        out = e.args[0]
    else:
        out = ""
        if consolebuffer:
            out += "".join(consolebuffer)
        if res:
            out += str(res) + "\n"
    del consolebuffer[:]
    return out
コード例 #6
0
ファイル: test_EmbeddedR.py プロジェクト: jjhoyt/gengis
 def tearDown(self):
     rinterface.setWriteConsole(rinterface.consolePrint)
     rinterface.setReadConsole(rinterface.consoleRead)
     rinterface.setReadConsole(rinterface.consoleFlush)