The Nextjournal R Environment
This article builds reusable environments for R runtimes, based on a minimal Bash environment. The R version installed is
library(dplyr) installed.packages() %>% as_tibble() %>% select(Package, Version)
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 Import 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. Setup
The full setup starting from a minimal Ubuntu installation is below
1.1. Build a Minimal R Environment
Install R from the R 3.5 repository for Ubuntu. build-essential is a necessary pre-requisite for installation of some R 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"))'
1.2. Build the Default R Environment
1.2.1. Install
Install the rest of the build tools, some required libraries, and other libraries that R needs 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-core 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", "showtext"))
Plotly
will complain without the dev version of ggplot2
.
devtools::install_github("hadley/ggplot2")
Set up to load basic font families on startup.
echo ' local({ suppressMessages(library(showtext)) # function to clean things up font_qattf <- function(family, base, reg, bd, it, bdit) { font_add(family, regular=paste0(base,reg,".ttf"), bold=paste0(base,bd,".ttf"), italic=paste0(base,it,".ttf"), bolditalic=paste0(base,bdit,".ttf")) } # add all basic custom families font_qattf("Arial", "Aerial", "", "Bd", "It", "BdIt") font_add("Times", regular="Tymes.ttf", bold="TymesBd.ttf") font_qattf("Verdana", "Veranda", "", "Bd", "It", "BdIt") font_qattf("DejaVu Sans", "DejaVuSans", "", "-Bold", "-Oblique", "-BoldOblique") font_qattf("DejaVu Sans Condensed", "DejaVuSansCondensed", "", "-Bold", "-Oblique", "-BoldOblique") font_qattf("DejaVu Sans Mono", "DejaVuSansMono", "", "-Bold", "-Oblique", "-BoldOblique") font_qattf("DejaVu Serif", "DejaVuSerif", "", "-Bold", "-Italic", "-BoldItalic") font_qattf("DejaVu Serif Condensed", "DejaVuSerifCondensed", "", "-Bold", "-Italic", "-BoldItalic") font_qattf("Liberation Mono", "LiberationMono-", "Regular", "Bold", "Italic", "BoldItalic") font_qattf("Liberation Sans", "LiberationSans-", "Regular", "Bold", "Italic", "BoldItalic") font_qattf("Liberation Serif", "LiberationSerif-", "Regular", "Bold", "Italic", "BoldItalic") font_qattf("Liberation Mono", "LiberationMono-", "Regular", "Bold", "Italic", "BoldItalic") font_qattf("Open Sans", "OpenSans-", "Regular", "Bold", "Italic", "BoldItalic") font_qattf("PT Sans", "PT_Sans-Web-", "Regular", "Bold", "Italic", "BoldItalic") font_qattf("PT Serif", "PT_Serif-Web-", "Regular", "Bold", "Italic", "BoldItalic") font_qattf("Roboto", "Roboto-", "Regular", "Bold", "Italic", "BoldItalic") font_qattf("Roboto Condensed", "RobotoCondensed-", "Regular", "Bold", "Italic", "BoldItalic") font_qattf("Roboto Mono", "RobotoCondensed-", "Regular", "Bold", "Italic", "BoldItalic") })' >> /etc/R/Rprofile.site
1.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"]]
2. Showcase
The following packages are included in Nextjournal's R 3.5 environment.
2.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
2.2. Plotting
Three plotting methods are available in the default environment. The built-in R plotting system works automatically to create static plots, Plotly vggplotly()
function, which will add some interactivity.
2.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
2.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')
2.2.3. ggplot2
Plot with the ggplot2 package.
library(ggplot2) qplot(speed, dist, data=cars) + geom_smooth()