/usr/lib/python2.7/dist-packages/rpy2/ipython/ggplot.py is in python-rpy2 2.4.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | """ Goodies for ipython """
from rpy2 import robjects
from rpy2.robjects.packages import importr
from rpy2.robjects.lib import ggplot2
from IPython.core.display import Image
import tempfile
grdevices = importr('grDevices')
# automatic plotting of ggplot2 figures in the notebook
class GGPlot(ggplot2.GGPlot):
# special representation for ipython
def _repr_png_(self, width = 700, height = 500):
# Hack with a temp file (use buffer later ?)
fn = tempfile.NamedTemporaryFile(mode = 'wb', suffix = '.png',
delete = False)
fn.close()
grdevices.png(fn.name, width = width, height = height)
self.plot()
grdevices.dev_off()
import io
with io.OpenWrapper(fn.name, mode='rb') as data:
res = data.read()
return res
def png(self, width = 700, height = 500):
""" Build an Ipython "Image" (requires iPython). """
return Image(self._repr_png_(width = width, height = height),
embed=True)
ggplot = GGPlot.new
class GGPlotSVG(ggplot2.GGPlot):
""" The embedding of several SVG figures into one ipython notebook is
giving garbled figures. The SVG functionality is taken out to a
child class.
"""
def _repr_svg_(self, width = 6, height = 4):
# Hack with a temp file (use buffer later ?)
fn = tempfile.NamedTemporaryFile(mode = 'wb', suffix = '.svg',
delete = False)
fn.close()
grdevices.svg(fn.name, width = width, height = height)
self.plot()
grdevices.dev_off()
import io
with io.OpenWrapper(fn.name, mode='rb') as data:
res = data.read().decode('utf-8')
return res
def svg(self, width = 6, height = 4):
""" Build an Ipython "Image" (requires iPython). """
return Image(self._repr_svg_(width = width, height = height),
embed=True)
|