def run(self): # Module on demand # from rpy2 import robjects as ro # Load dataframe # ro.r("library(vegan)") ro.r("table = read.table('%s', sep='\t', header=TRUE, row.names='X')" % (self.csv)) # Run computation # if self.calc_distance: ro.r("nmds = metaMDS(table, distance='horn', trymax=200)") else: ro.r("nmds = metaMDS(table, trymax=200)") # Extract result # ro.r("coord = scores(nmds)") ro.r("loadings = nmds$species") # Retrieve values # self.coords = r_matrix_to_dataframe(ro.r.coord) # No loadings without distance # if self.calc_distance: self.loadings = r_matrix_to_dataframe(ro.r.loadings) else: self.loadings = False # Plot it # self.graph.plot()
def run_df(self): """Unfortunately this doesn't seem to work (yet)""" # Module on demand # from rpy2 import robjects as ro # Get frame # self.frame = self.parent.parent.frame # Call R # rframe = pandas_df_to_r_df(self.frame) ro.r("library(vegan)") nmds = ro.r['metaMDS'](rframe, distance='horn', trymax=200) # Retrieve values # self.coords = r_matrix_to_dataframe(ro.r['scores'](nmds)) self.loadings = list(nmds.rx2('species')) # Plot it # self.graph.plot()