R

The :r flavor of Carto-Lab Docker provides a pre-configured environment for spatial data science, statistical computing, and cartographic visualization in R.

Key packages included:


Usage

Add the R flavor overlay to your .env file:

COMPOSE_FILE=docker-compose.yml:docker-compose.r.yml
COMPOSE_PATH_SEPARATOR=:

Then start the container:

docker compose up -d
Have a look at the docker-compose.r.yml overlay

services:
  jupyterlab:
    image: quay.io/ioer-fdz/carto-lab-docker:${TAG:-r}
    build:
      context: ./r
      args:
        VERSION: ${TAG:-latest}
See the r/environment_r.yml file for the full list of packages in the R environment

name: r_env
channels:
  - conda-forge
dependencies:
  - r-base
  - r-caret
  - r-crayon
  - r-dplyr
  - r-devtools
  - r-e1071
  - r-forecast
  - r-ggplot2
  - r-hexbin
  - r-htmltools
  - r-htmlwidgets
  - r-irkernel
  - r-maps
  - r-mapdata
  - r-tmap
  - r-nycflights13
  - r-randomforest
  - r-raster
  - r-rastervis
  - r-rcurl
  - r-rcolorbrewer
  - r-remotes
  - r-reshape
  - r-rmarkdown
  - r-rodbc
  - r-rsqlite
  - r-scales
  - r-sf
  - r-stringr
  - r-shiny
  - r-terra
  - r-tidymodels
  - r-tidyverse
  - unixodbc
  - jupyter_client
See the r/Dockerfile for the R environment build specifications

# install r-studio, based on:
# https://github.com/jupyter/docker-stacks/blob/main/images/r-notebook/Dockerfile

# build time args
ARG VERSION=latest
ARG ENVIRONMENT_FILE=environment_r.yml

## public:
# FROM quay.io/ioer-fdz/carto-lab-docker:$VERSION
## private:
FROM gcr.hrz.tu-chemnitz.de/ioer/fdz/carto-lab-docker:$VERSION

# select default shell
SHELL ["/bin/bash", "-c"]

ENV ENVIRONMENT_FILE=environment_r.yml \
    R_ENV_NAME=r_env
ENV CONDA_ACTIVATE_PATH=/opt/conda/bin/activate \
    JUPYTER_ENV_PATH=/opt/conda/envs/jupyter_env/ \
    R_ENV_PATH=/opt/conda/envs/$R_ENV_NAME/

COPY $ENVIRONMENT_FILE $ENVIRONMENT_FILE

ENV R_BINDINGS=" \
    fonts-dejavu \
    unixodbc \
    unixodbc-dev \
    r-cran-rodbc \
    gfortran \
    gcc"

# R pre-requisites
RUN apt-get update --yes \
    && apt-get install --yes --no-install-recommends \
    $R_BINDINGS \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

# install user kernel environment (r_env)
RUN CONDA_SOLVER=rattler conda env create --file $ENVIRONMENT_FILE --name $R_ENV_NAME --quiet \
    && source $CONDA_ACTIVATE_PATH $R_ENV_PATH \
    && Rscript -e "IRkernel::installspec(name = 'r_env', displayname = 'R (r_env)', user = FALSE)" \
    && mamba clean --all --force-pkgs-dirs --yes

# Configure native JupyterLab 4 R text replacement shortcuts (RStudio style)
# Alt - -> "<- "
# Accel Shift M (Ctrl+Shift+M / Cmd+Shift+M) -> "%>% "
RUN jq '. + { \
  "@jupyterlab/shortcuts-extension:shortcuts": { \
    "shortcuts": [ \
      { \
        "command": "apputils:run-first-enabled", \
        "selector": "body", \
        "keys": ["Alt -"], \
        "args": { \
          "commands": ["console:replace-selection", "fileeditor:replace-selection", "notebook:replace-selection"], \
          "args": {"text": "<- "} \
        } \
      }, \
      { \
        "command": "apputils:run-first-enabled", \
        "selector": "body", \
        "keys": ["Accel Shift M"], \
        "args": { \
          "commands": ["console:replace-selection", "fileeditor:replace-selection", "notebook:replace-selection"], \
          "args": {"text": "%>% "} \
        } \
      } \
    ] \
  } \
}' /opt/conda/envs/jupyter_env/share/jupyter/lab/settings/overrides.json > /tmp/overrides.json \
  && mv /tmp/overrides.json /opt/conda/envs/jupyter_env/share/jupyter/lab/settings/overrides.json

Building the Image Locally

If you want to build the :r flavor locally against your target base image:

docker compose -f docker-compose.yml -f docker-compose.r.yml build \
    --no-cache --progress=plain \
  && docker compose -f docker-compose.yml -f docker-compose.r.yml up -d

Optionally push to a registry:

docker compose -f docker-compose.yml -f docker-compose.r.yml push

See the developer section for more details on version tagging and distributing custom builds.