The Nextjournal R Environment

Default environments for R 3.5.

This article builds reusable environments for R runtimes, based on a minimal Bash environment. The exact R version installed is nil.

To make use of an environment from this article, follow these steps:

  • Create a new article, and insert an R cell.
  • Activate the runtime settings in the sidebar (see image below).
  • Bring up the Environments dropdown.
  • Select the Transclude environment… item.
  • Search for this article, nextjournal/r-environment, by entering 'r-env' into the search field.
  • Select it to list all environments within.
  • Select the desired environment.

1.
Showcase

The following packages are included in Nextjournal's R 3.5 environment.

1.1.
System Packages and Basics

A number of support libraries are installed, as well as gcc v7 for installing Rcpp and other packages that require compilation.

The Rcpp package version nil allows for tight integration between R and C++ codes and libraries. The Tidyverse is also installed—this is a set of several data science packages which all follow certain design, grammar, and structural characteristics.

1.2.
Plotting

Three plotting methods are available in the default environment. The built-in R plotting system works automatically to create static plots, Plotly vnil provides full access to the functionality of Plotly on R to generate figures with basic interactivity, and ggplot2 vnil syntax can be used either statically or with Plotly's ggplotly() function, which will add some interactivity.

1.2.1.
R

x <- seq(-10, 10, length= 60); y <- x
f <- function(x, y) { r <- sqrt(x^2+y^2); sin(r)/r }
z <- outer(x, y, f)
persp(x, y, z, theta = 30, phi = 30, expand = 0.5, col = "#009caa"); NULL

1.2.2.
Plotly

This syntax allows full access to the functionality of Plotly on R.

library(plotly)

d <- diamonds[sample(nrow(diamonds), 1000), ]
plot_ly(d, x = ~carat, y = ~price,
  # Hover text:
  text = ~paste("Price: ", price, '$<br>Cut:', cut),
  color = ~carat, size = ~carat, type='scatter', mode='markers')

1.2.3.
ggplot2

Plot with the ggplot2 package.

library(ggplot2)

qplot(speed, dist, data=cars) + geom_smooth()

2.
Setup

2.1.
Build a Minimal R Environment

Install R from the R 3.5 repository for Ubuntu. We need to install build-essential in order to build two packages.

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 \
  build-essential \
  r-base
apt-get clean
rm -r /var/lib/apt/lists/* # Clear package list so it isn't stale

Setup package install options.

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"))'

2.2.
Build the Default R Environment

2.2.1.
Install

Install the rest of the build tools, some require libraries, and anything else R wants for development.

apt-get -qq update
apt-get install --no-install-recommends \
  gfortran cmake automake libtool libltdl-dev pkg-config \
  libssh2-1 libcurl4-openssl-dev libssl-dev libxml2-dev libcairo2-dev \
  r-base-dev
apt-get clean
rm -r /var/lib/apt/lists/* # Clear package list so it isn't stale

This default image has support for tidyverse, plotly, and svglite. The package also makes some basic utilities available, as well as devtools to help fascilitate the use of certain packages.

install.packages(c("feather", "devtools", "tidyverse", "plotly", "svglite"))

Plotly will complain without the dev version of ggplot2.

devtools::install_github("hadley/ggplot2")

2.2.2.
Test

strsplit(R.version.string," ")[[1]][[3]]
packageDescription("Rcpp")[["Version"]]
packageDescription("tidyverse")[["Version"]]
packageDescription("plotly")[["Version"]]
packageDescription("ggplot2")[["Version"]]
packageDescription("jsonlite")[["Version"]]
packageDescription("svglite")[["Version"]]
packageDescription("feather")[["Version"]]
© 2018 Nextjournal GmbH