# Using an R Container for Analytical Models

DevFeed: [Using an R Container for Analytical Models](<https://devfeed.tech/articles/using-an-r-container-for-analytical-models-35187.md>)

Original publisher: [Read original article](<https://blog.jessfraz.com/post/r-containers-for-data-science/>)

Published: 2015-06-30T15:25:24Z

Content type: tutorial

Language: en

Sources: [Jessie Frazelle](<https://devfeed.tech/sources/jessie-frazelle.md>)

Topics: [R](<https://devfeed.tech/topics/r.md>), [Docker](<https://devfeed.tech/topics/docker.md>), [Data Science](<https://devfeed.tech/topics/data-science.md>), [plotting](<https://devfeed.tech/topics/plotting.md>), [Docker Image](<https://devfeed.tech/topics/docker-image.md>)

Tags: [container](<https://devfeed.tech/tags/container.md>), [data-science](<https://devfeed.tech/tags/data-science.md>), [docker](<https://devfeed.tech/tags/docker.md>), [docker-image](<https://devfeed.tech/tags/docker-image.md>), [plotting](<https://devfeed.tech/tags/plotting.md>), [python](<https://devfeed.tech/tags/python.md>)

## AI overview

A tutorial on building and using an R Docker container for data science. It covers creating a custom image from an R base image, installing selected R packages, running the container with mounted data and X11 display support, and plotting the iris dataset with ggplot2.

## Source excerpt

So it turns out I'm pretty bad at vacation. I had this idea for a blog post and one thing lead to another and here we are... You probably know by now I hate installing things on my host. At my previous job we did a lot of work with using Python and R for data science. I still love plotting data with ggplot and my favorite R package, wes anderson color palette. Here's a fast intro into how to do this with an R Docker image. Now everyone loves their share of different packages, without a doubt I bet most of them are written by Hadley Wickham ;). Can you imagine if the percentage of packages contributed by Hadley to CRAN was mirrored by someone to NPM or pip? It would be crazy. We are going to start with an R base and build our ideal (aka you can make yours different, chill...) R data science container, with the following Dockerfile: # our R base image FROM r-base # install packages # these are ones I like RUN echo 'install.packages(c("ggplot2", "plyr", "reshape2", "RColorBrewer", "scales","grid", "wesanderson"), repos="http://cran.us.r-project.org", dependencies=TRUE)' > /tmp/packages.R \ && Rscript /tmp/packages.R # create an R user ENV HOME /home/user RUN useradd --create-home --home-dir $HOME user \ && chown -R user:user $HOME WORKDIR $HOME USER user # set the command CMD ["R"] Build the image: $ docker build --rm --force-rm -t jess/r-custom . Run and use the image: # we need X11 for the graph to display, alternatively # you can save to a file that is in a bind-mounted dir # or you can docker cp the file to the host :) $ docker run -it --name analytics \ -v /tmp/.X11-unix:/tmp/.X11-unix \ -e DISPLAY=unix$DISPLAY \ jess/r-custom # bind mount your data $ docker run -v $(pwd)/data:/home/user/data \ -it --name analytics \ -v /tmp/.X11-unix:/tmp/.X11-unix \ -e DISPLAY=unix$DISPLAY \ jess/r-custom Now plot something: library(wesanderson) library(ggplot2) ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) + geom_point(size = 3) + scale_color_manual(values = wes_pal