3 R packages
To install extra R packages in your Docker image, use install.R
and Rscript
in your Docker file.
COPY install.R /tmp/install.R
RUN Rscript /tmp/install.R
Make sure to install to "${R_HOME}/site-library"
since by default install.packages()
will install to the user library in /home
and that will be replaced with the user home directory in Jupyter Hub with a persistent home directory.
install.R
repo <- "https://p3m.dev/cran/__linux__/jammy/2024-05-13"
lib <- "${R_HOME}/site-library"
list.of.packages <- c("ggplot2","remotes", lib=lib)
install.packages(list.of.packages, repos=repo, lib=lib) remotes::install_github("hadley/httr@v0.4", lib=lib)
You can also use the helper script which make sure packages go to the site-library:
COPY . /tmp2/
RUN /pyrocket_scripts/install-r-packages.sh /tmp2/install.R
3.0.1 Spatial libraries
Some packages depend on linux packages. One example are spatial packages like sf
which depend on GDAL. In this case
install.packages("sf")
will not work because it will not install the linux package dependencies.
There are a few ways to get around this.
Install the necessary linux packages via apt-get. This can be hard.
Install via via /rocker_scipts/install_geospatial.sh To do this include
RUN echo '.libPaths(file.path(Sys.getenv("R_HOME"), "site-library"))' > /tmp/rprofile.site RUN env R_PROFILE=/tmp/rprofile.site \ PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \ /rocker_scripts/install_geospatial.sh RUN rm /tmp/rprofile.site
in your Dockerfile. The extra code with a temporary
R_PROFILE
makes sure everything is installed to"${R_HOME}/site-library"
and that the PATH does not have conda on it, which would break the needed linux installs.Use r2u which has Ubuntu binaries with all the dependencies included.
3.0.2 Default CRAN repository
The default CRAN repository is set in ${R_HOME}/etc/Rprofile.site
and the CRAN environmental variable is set to the date pinned version associated with the R version in the image. For example, https://p3m.dev/cran/__linux__/jammy/2024-10-30
.
To set a different repo, edit ${R_HOME}/etc/Rprofile.site
if you are changing an image or ~/.Rprofile
if setting user defaults:
options(repos = c(CRAN = 'https://p3m.dev/cran/__linux__/jammy/latest'), download.file.method = 'libcurl')
You can also specify the repo to use in install.packages()
like so
repo <- "https://p3m.dev/cran/__linux__/jammy/latest"
list.of.packages <- c("maps", "mapdata", "RColorBrewer") install.packages(list.of.packages, repos=repo)