Using rpy2 with IPython laptops?

Is it possible to use rpy2 (calling ggplot2) with IPython laptops and then save them (and share with NBViewer, like other laptops http://nbviewer.ipython.org/ )? Is there a problem with rpy2 ggplots appearing on a laptop and / or interactively? It would be helpful if someone could provide an example session and its conclusion about creating a ggplot2 figure in a laptop using rpy2 in IPython.

+5
source share
2 answers

This was written without looking at the code in rmagic. They have a smarter way to do this (I have 11 lines of code).

import uuid
from rpy2.robjects.packages import importr 
from IPython.core.display import Image

grdevices = importr('grDevices')
def ggplot_notebook(gg, width = 800, height = 600):
    fn = '{uuid}.png'.format(uuid = uuid.uuid4())
    grdevices.png(fn, width = width, height = height)
    gg.plot()
    grdevices.dev_off()
    return Image(filename=fn)

To try:

from rpy2.robjects.lib import ggplot2
from rpy2.robjects import Formula
datasets = importr('datasets')
mtcars = datasets.__rdata__.fetch('mtcars')['mtcars']
p = ggplot2.ggplot(mtcars) + \
    ggplot2.aes_string(x='mpg', y='cyl') + \
    ggplot2.geom_point() + \
    ggplot2.geom_smooth() + \
    ggplot2.facet_wrap(Formula('~ am'))

ggplot_notebook(p, height=300)
+3
source

rmagic, rpy2. , print() . : http://nbviewer.ipython.org/5029692

rpy2, . rpy2 ggplot2. , PNG/SVG, Python ( , rmagic).

+1

All Articles