David Schmudde / Jul 03 2019

RPY2

rpy2

import rpy2.robjects.packages as rpackages
import rpy2.robjects as ro
rpackages.importr("ggplot2")
R = ro.r
datasets = rpackages.importr('datasets')
mtcars = rpackages.data(datasets).fetch('mtcars')['mtcars']
gp = ggplot2.ggplot(mtcars)
pp = (gp 
      + ggplot2.aes_string(x='wt', y='mpg')
      + ggplot2.geom_point(ggplot2.aes_string(colour='qsec'))
      + ggplot2.scale_colour_gradient(low="yellow", high="red") 
      + ggplot2.geom_smooth(method='auto') 
      + ggplot2.labs(title="mtcars", x='wt', y='mpg'))

pp.plot()
# R.head(mtcars)
# R("dev.copy(png,'/results/out.png')")
pp.plot()._repr_png_

rpy2 Install

Install R from the R 3.5/3.6 repository for Ubuntu. The build-essential package (which includes gcc) is a necessary prerequisite for installation of some R packages but is already included in the Python base.

echo 'deb https://cloud.r-project.org/bin/linux/ubuntu bionic-cran35/' > \
  /etc/apt/sources.list.d/r35.list
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E084DAB9

apt-get -qq update
DEBIAN_FRONTEND=noninteractive \
  apt-get install --no-install-recommends \
  r-base
apt-get clean
rm -r /var/lib/apt/lists/* # Clear package list so it isn't stale
echo 'local({
  r <- getOption("repos")
  r["CRAN"] <- "https://cloud.r-project.org"
  options(repos = r, download.file.method = "libcurl")
})' > /etc/R/Rprofile.site

Install two necessary packages for R to work on Nextjournal.

R -e 'install.packages(c("base64enc", "jsonlite"), Ncpus=4)'
pip install rpy2
import rpy2
print(rpy2.__version__)
# import rpy2's package module
import rpy2.robjects.packages as rpackages

# import R's utility package
utils = rpackages.importr('utils')

# select a mirror for R packages
utils.chooseCRANmirror(ind=1) # select the first mirror in the list

utils.install_packages('ggplot2')
<rpy2.rinterf...RTYPES.NILSXP]