diff --git a/Dockerfile.Full b/Dockerfile.Full new file mode 100644 index 0000000..515a3dd --- /dev/null +++ b/Dockerfile.Full @@ -0,0 +1,32 @@ +#Date Created: Jan 8, 2026 +#Author Alex Gebben +FROM rocker/r-ver:4.5.2 +LABEL org.opencontainers.image.authors=agebben@uwyo.edu +RUN apt-get update \ +&& apt-get install -y sudo +#Create a sudo user not called root. This avoids permision issues when working with the files created in the docker and saved on a local unix system + #RUN useradd -m -s /bin/bash rdocker + RUN adduser --disabled-password --gecos '' rdocker \ + && adduser rdocker sudo \ + && echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers \ + && chmod 777 /usr/local/lib/R/site-library + USER rdocker +#Install all necessary packages for anaylysis, file manipulation, or for the required R packages to function. +RUN sudo apt-get install -y libmariadb-dev imagemagick ghostscript libpoppler-cpp-dev vim wget curl git libcurl4-openssl-dev build-essential texlive-full +#Install the R packages used in the R data creation scripts run on on the docker container. +RUN install2.r tidyverse fixest twang pdftools gt scales vars curl forecast uuid corrplot foreach cluster factoextra xtable Formula miscTools MASS kableExtra clisymbols fredr readxl lubridate tidycensus zipcodeR stargazer sandwich lmtest ggnewscale viridis ggthemes MatchIt optmatch aTSA tsDyn bruceR svars tidyquant zoo eia renv BiocManager miscTools logr && rm -rf /tmp/downloaded_packages + #Packages used in DCchoice nuclear contigent valuation paper which require a special install method + #Icens is required to be installed using BiocManager for DCchoice to be installed. + #"interval" required Icens to be installed + #BiocManager Version will need to be updated periodically. + RUN R -e 'BiocManager::install(version = "3.22")'\ + && R -e 'BiocManager::install("Icens")' \ + && R -e "install.packages('DCchoice',dependencies=TRUE)" \ + && install2.r interval \ + && rm -rf /tmp/downloaded_packages +WORKDIR /R-Code +RUN sudo chmod -R 777 /R-Code +#Add VIM R IDE to work in the Container, not required. + #Packages used for a vim-R IDE. Can be removed if not used + #Add vimrc which can be used with vimplug to run an R ide to perform R edits in the terminal. + ADD VIM_R_IDE/.vimrc /home/rdocker/.vimrc diff --git a/Dockerfile.Minimal b/Dockerfile.Minimal new file mode 100644 index 0000000..d404271 --- /dev/null +++ b/Dockerfile.Minimal @@ -0,0 +1,21 @@ +#Date Created: Jan 8, 2026 +#Author Alex Gebben +FROM rocker/r-ver:4.5.2 +LABEL org.opencontainers.image.authors=agebben@uwyo.edu +#Create a sudo user not called root. This avoids permision issues when working with the files created in the docker and saved on a local unix system + #RUN useradd -m -s /bin/bash rdocker +RUN apt-get update \ +&& apt-get install -y sudo\ + && adduser --disabled-password --gecos '' rdocker \ + && adduser rdocker sudo \ + && echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers \ + && chmod 777 /usr/local/lib/R/site-library + USER rdocker +#Install all necessary packages for anaylysis, file manipulation, or for the required R packages to function. +RUN sudo apt-get install -y vim curl git && install2.r dplyr renv && rm -rf /tmp/downloaded_packages +WORKDIR /R-Code +RUN sudo chmod -R 777 /R-Code +#Add VIM R IDE to work in the Container, not required. + #Packages used for a vim-R IDE. Can be removed if not used + #Add vimrc which can be used with vimplug to run an R ide to perform R edits in the terminal. + ADD VIM_R_IDE/.vimrc /home/rdocker/.vimrc diff --git a/Make_Docker.sh b/Make_Docker.sh new file mode 100644 index 0000000..0de2e4e --- /dev/null +++ b/Make_Docker.sh @@ -0,0 +1,13 @@ +# +ALL_PACKAGES=true +MINIMAL_PACKAGES=false +#Load all R packages in the docker +if [ "$ALL_PACKAGES" = true ]; then + docker build --file Dockerfile.Full -t acg/r_full . + docker build --file Dockerfile.Full -t acg/r_full:1.0 . +fi +##Minimal +if [ "$MINIMAL_PACKAGES" = true ]; then + docker build --file Dockerfile.Minimal -t acg/r_minimal . + docker build --file Dockerfile.Minimal -t acg/r_minimal:1.0 . +fi diff --git a/README.md b/README.md index 57d55d6..3f739c7 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,6 @@ # R-Docker-Starting-point +A starting point to run R in docker with renv for publication reproducibility. An IDE is installed to allow for R editing on any computer. The container comes from the docker hub Rocker r-sources. Customization is made to run a vim IDE of R, and to load libraries used in my workflow. + +1) If the r docker images have not been created run the "Make_Docker.sh" file. +2) run "source Alias_Docker.sh" to add terminal shortcuts for running the docker. These are r-full or r-minimal for entering the docker container to work in r, and r-full-script or r-minimal-script for running a local script in the container without any interaction. These commands can be copied to bash_alias -A starting point to run R in docker with renv for publication reproducibility. An IDE is installed to allow for R editing on any computer. \ No newline at end of file diff --git a/VIM_R_IDE/.vimrc b/VIM_R_IDE/.vimrc new file mode 100644 index 0000000..0fa2b9e --- /dev/null +++ b/VIM_R_IDE/.vimrc @@ -0,0 +1,14 @@ +if empty(glob('~/.vim/autoload/plug.vim')) + silent !curl -fLo ~/.vim/autoload/plug.vim --create-dirs + \ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim + autocmd VimEnter * PlugInstall --sync | source $MYVIMRC +endif +" Specify a directory for plugins +" - For Neovim: stdpath('data') . '/plugged' +" - Avoid using standard Vim directory names like 'plugin' +call plug#begin('~/.vim/plugged') + Plug 'jalvesaq/Nvim-R' + Plug 'frazrepo/vim-rainbow' +call plug#end() +let g:rainbow_active = 1 +" Initialize plugin system diff --git a/VIM_R_IDE/README_VIM.md b/VIM_R_IDE/README_VIM.md new file mode 100644 index 0000000..9c17b19 --- /dev/null +++ b/VIM_R_IDE/README_VIM.md @@ -0,0 +1,4 @@ +This file contains a ".vimrc" file that can be loaded to the /root/ directory. Then when using vim it will download and install vimplug, as well as the nvimr plugin. This allows vim to act as an IDE, so the code can be configures in the docker image, and not simply run with the script. + +If this plugin fails to load it can be commented out in the Dockerfile (make script at the end). This is a personal preference about how to edit the code. + diff --git a/r-Docker-Alias.sh b/r-Docker-Alias.sh new file mode 100644 index 0000000..c926bf6 --- /dev/null +++ b/r-Docker-Alias.sh @@ -0,0 +1,9 @@ +#Create a terminal shortcut to open the current directory in the docker +#Docker images with all the R packages I have ever used +alias r-full='docker run -ti --rm -v ./:/R-Code/ acg/r_full:latest bash' #Pull in the current directory into the docker image. Enter the image to modify with a r vim IDE preloaded. +alias r-full-script='docker run -v ./:/R-Code/ acg/r_full:latest Rscript' #Run a r-file as a script rather than Interactively + +#Docker images with minimal packages loaded to save space +alias r-min='docker run -ti --rm -v ./:/R-Code/ acg/r_minimal:latest bash' +alias r-min-script='docker run -v ./:/R-Code/ acg/r_minimal:latest Rscript' +