Ejemplo n.º 1
0
    def render(self, dataframe, path ):

        R.library( 'ggplot2' )

        rframe = pandas.rpy.common.convert_to_r_dataframe(dataframe)
        
        # sometimes the row/column mapping did not work
        # rframe.colnames = dataframe.columns

        unAsIs = R('''function (x) {
                         if(typeof(x) %in% c("integer","double")) {
                             class(x) <- "numeric"
                             return (x)}
                         else if (typeof(x) == "character") {
                             class(x) <- "character"
                             return (x) }
                         else {
                             return(x) } }''')

        rframe = R["as.data.frame"](R.lapply(rframe,unAsIs))

        R.assign( "rframe", rframe )
        # start plot
        R('''gp = ggplot( rframe )''')

        # add aesthetics and geometries
        try:
            pp = R('''gp + %s ''' % self.statement )
        except ValueError as msg:
            raise ValueError( "could not interprete R statement: gp + %s; msg=%s" % (self.statement, msg ))
            
        figname = re.sub( '/', '_', path2str(path) )
        r = ResultBlock( '#$ggplot %s$#' % figname,
                         title = path2str(path) )
        r.rggplot = pp
        r.figname = figname

        return ResultBlocks( r )