Tutorial 4
Virtual environments and docker containers. We’ll practice creating virtual environments again (just like last tutorial), so we can compare it with creating docker containers.
R Environments
- Create a new RStudio Project
 - Note the currently installed packages
 - Install the 
renvlibrary if you do not have it installed - Initialize the current project as an 
renvproject - Note the new packages in the environment.
 - Install an R package
 - Snapsnot the package
 - Note how the 
renv.lockfile does not change - Create and save an R script that uses the package you just installed
 - Re-snapshot the project
 - Note how the 
renv.lockfile updates now - Close the current RStudio project
 - Note how you have your old packages back
 - Re-open the RStudio project you just created and see how t5he packages change.
 
Optional: Python environment
For people and teams working on a python project and want to practice conda environments.
conda environments are not going to be on the exam. This exercise is solely to give you instructions to practice creating python environments.
- Check that you are currently in the 
baseconda environment: 
conda info- Note the packages in the current conda environment and their versions
 
conda list- Create a new conda environment (we are going to name it 
myenv) with python 3.12 
conda create -n myenv -c conda-forge python=3.12- Activate this environment
 - Run python and compare your python versions and see if you are able to import 
pandas condainstall another package into the environment,pandasand import it from the new environment.
Run a docker container
Let’s get some practice with running docker containers
Docker Desktop needs to be runnign in the background.
Your first container
We did this in the Computer Setup process, but let’s run the hello-world container with docker run
$ docker run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/
For more examples and ideas, visit:
 https://docs.docker.com/get-started/
$This command runs a container that executes a script that prints out the hello docker text. When this script runs, it exits and the container exits to give you your prompt back
An R Container
- Compare the version of R you have right now on your computer. You can open up and launch 
Ror runR --versionto get your current R version. 2.Run the R 4.3.2 container: 
docker run --rm -it rocker/r-ver:4.3.2- Compare the version from the version you have on your computer
 - Try to load a package you have installed on your computer in the container (it should fail)
 - Exit out of this docker container
 
An RStudio Container
If you are on an ARM based M-chip mac, you need to pass in the --platform=linux/amd64 flag to the docker run command. https://ubc-dsci.github.io/reproducible-and-trustworthy-workflows-for-data-science/lectures/110-containerization-2.html#specifying-the-image-architectureplatform
- Run the RStudio container and connect it to your local computer’s port 
8787 
docker run --rm -e PASSWORD="pizza" -p 8787:8787 rocker/rstudio:4.4.2Explain in your own words what each of the flags are doing in the
docker runcommand. You can use the officialdocker rundocumentation: https://docs.docker.com/reference/cli/docker/container/run/Open and log into the RStudio container and try to run some R code in a script
Save this script
Close the container and run it again
Notice how all your work inside the container is lost
An RStudio Tidyverse Container
- Do the same process as before, but this time use the 
rocker/tidyverse:4.4.2container. - Without turning off this container, open a new terminal and run a separate 
rocker/rstudio:4.4.2container 
- You will need to change the local port mapping
 
- Make sure you are able to log into and run both RStudio sessions in the container
 
- Note how only one of the RStudio sessions has 
tidyverseinstalled and the other doesn’t