Ejemplo n.º 1
0
 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)
Ejemplo n.º 2
0
    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))
Ejemplo n.º 3
0
    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
Ejemplo n.º 4
0
    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
Ejemplo n.º 5
0
        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
Ejemplo n.º 6
0
 def tearDown(self):
     rinterface.setWriteConsole(rinterface.consolePrint)
     rinterface.setReadConsole(rinterface.consoleRead)
     rinterface.setReadConsole(rinterface.consoleFlush)