Exemplo n.º 1
0
    def sync_with_r(self):
        '''Query the rsession for all objects and try to recreate using the rsession wrapper objects'''
        for obj in rsession.r.ls():
            #try:
                pyobj = rsession.ro.globalEnv.get(str(obj))
                typeof = rsession.r['class'](pyobj)[0]
                #There are also multivariable time series (is_mts) that we should worry about
                #Converting arrays to dataframes flattens the array
                if typeof == 'table':
                    info('FYI, the array object you selected is being imported as a flattened dataframe.')
                    pyobj = rsession.df(pyobj)

                if typeof == 'ts':
                    #Strip all the data needed to recreate the object
                    start = rsession.r['start'](pyobj)
                    end = rsession.r['end'](pyobj)
                    frequency = rsession.r['frequency'](pyobj)
                    times = rsession.r['time'](pyobj)
                    deltat = rsession.r['deltat'](pyobj)
                    data = numpy.array(pyobj).T
                    label = str(obj)
                    self.render_timeseries(data,label=label,start=start,end=end,frequency=frequency,deltat=deltat)

                if typeof == 'data.frame':
                    data = numpy.array(pyobj).T
                    rownames=pyobj.rownames()
                    columns = odict()
                    for col,dat in zip(pyobj.colnames(),pyobj):
                        ptype = rsession.typeof(dat)
                        columns[col] = rsession.translate_types(ptype)
                    self.render_dataframe(data,columns,name=str(obj),rownames=rownames)

                if typeof == 'htest':
                    self.render_description(pyobj,str(obj))

                if typeof == 'lm':
                    self.render_linear_model(pyobj,'Linear Model')

                if typeof == 'nls':
                    self.render_linear_model(pyobj,'Nonlinear Model')

                #Vectors
                if typeof == 'numeric':
                    #Cast into dataframe
                    pyobj = rsession.df(pyobj)
                    data = numpy.array(pyobj).T
                    rownames=pyobj.rownames()
                    columns = odict()
                    for col,dat in zip(pyobj.colnames(),pyobj):
                        ptype = rsession.typeof(dat)
                        columns[col] = rsession.translate_types(ptype)
                    self.render_dataframe(data,columns,name=str(obj),rownames=rownames)
                if config.__devel__:
                    print('Tring to import to workspace. \n'+str(type(pyobj)) + '\n')
Exemplo n.º 2
0
    def render_timeseries(self,data,label=None,start=None,end=None,frequency=None,deltat=None,times=None):
        '''Time series are handled just like dataframes except we allow for options on frequency/start/end'''

        #This only works for ts, not mts at the moment
        ptype = rsession.typeof(data[0])
        columns = {'V1':rsession.translate_types(ptype)}

        if not label:
            inc = 0
            for unmes in self.robjects.keys():
                if 'unnamed' in unmes:
                    inc += 1
            name = 'unnamed' + str(inc+1)

        timeseries = rsession.timeseries(data,label=label,start=start,end=end,frequency=frequency,deltat=deltat)
        rownames = timeseries.rownames

        self.add_r_object(timeseries,label)
        self.set_active_robject(timeseries)
        self.handle_dataview(data,columns,rownames=rownames,editable=True)