def displayOpenairPlot(func, figsize=(10,10), res=150, *args, **kwargs): """ Displays openair plots inline. func. openair callback. figsize. tuple. figure size res. int. resolution of figure **kwargs. to be passed to func. -------------example of useage-------------------------------------- > from openairpy import OpenairPy > openair = OpenairPy.loadOpenair() > func = openair.corPlot > OpenairPy.displayOpenairPlot(func, mydata=df, dendrogram=True) -------------------------------------------------------------------- """ import IPython from rpy2.robjects.lib import grdevices pixel_per_inch = 0.0104166667 width, height =figsize[0]/pixel_per_inch, figsize[1]/pixel_per_inch with grdevices.render_to_bytesio(grdevices.png, width=width, height=height, res=res) as img: plot = func(*args, **kwargs) IPython.display.display(IPython.display.Image(data=img.getvalue(), format='png', embed=True)) return None
def _img(self): from rpy2.robjects.lib import grdevices from rpy2 import robjects with grdevices.render_to_bytesio(grdevices.png, type="cairo-png", width=self.width, height=self.height, res=self.dpi, antialias="subpixel") as b: robjects.r("print")(self.object) return b.getvalue()
def svg(self, width=6, height=4): """ Build an Ipython "Image" (requires iPython). """ with grdevices.render_to_bytesio(grdevices.svg, width=width, height=height) as b: robjects.r("print")(self) data = b.getvalue() ip_img = Image(data=data, format='svg', embed=False) return ip_img
def image_png(gg, width=800, height=400): with grdevices.render_to_bytesio(grdevices.png, type="cairo-png", width=width, height=height, antialias="subpixel") as b: robjects.r("print")(gg) data = b.getvalue() ip_img = Image(data=data, format='png', embed=True) return ip_img
def r_inline_plot(width=4, height=4, dpi=100): with grdevices.render_to_bytesio(grdevices.png, width=width, height=height, units='in', res=dpi) as b: yield data = b.getvalue() display(Image(data=data, format='png', embed=True))
def cellplot(width=600, height=600, dpi=100): """Função para permitir a visualização dos gráficos gerados no R com rpy2 nas células do Jupyter Notebook See: Código adaptado de: https://stackoverflow.com/questions/43110228/how-to-plot-inline-with-rpy2-in-jupyter-notebook """ with grdevices.render_to_bytesio(grdevices.png, width=width, height=height, res=dpi) as b: yield data = b.getvalue() display(Image(data=data, format='png', embed=True))
def test_rendertobytes_plot(): with grdevices.render_to_bytesio(grdevices.png) as b: r(''' plot(0) ''') assert len(b.getvalue()) > 0
def test_rendertobytes_noplot(): with grdevices.render_to_bytesio(grdevices.png) as b: pass assert len(b.getvalue()) == 0
def testRenderToBytesPlot(self): with grdevices.render_to_bytesio(grdevices.png) as b: r(''' plot(0) ''') self.assertTrue(len(b.getvalue()) > 0)
def testRenderToBytesNoPlot(self): with grdevices.render_to_bytesio(grdevices.png) as b: pass self.assertEqual(0, len(b.getvalue()))