R – Exercises
Part 1. R and R package versions
1.1 Using Linux "module" function to switch to different versions of R
1.2. Install a different version of R Package in your home directory
Part 2. R-shell/R-script through Docker
2.1 Start a Docker container using the image "rocker/r-ver" (v4.1.1) built by the Rocker project
2.2 Start an R-shell within container (replacing "xxxxxxx" with the container ID you get from last command )
Now you can run some R command. Remember, you are running as "root" user.
2.3 Start a BASH-shell within container (replacing "xxxxxxx" with the container ID you get from "docker1 ps" command)
Now you can run some BASH command. Again, you are the "root" user in container, you can do anything as "root".
Access files of the host machine. The /workdir in the container is the same as /workdir/$USER of the host.
Exit the container BASH
2.4 Save the modified container to a new image file.
Now that you have installed some R packages and software into the container. You might want to save this container as a new image file. Otherwise if the container gets killed, all the newly installed software will be gone.
Export the image as a file
Once the file is saved, at another time and/or on another computer, you can load this image again.
2.5 Clean up
Check all containers and images on the host
Remove all the containers created by you. To remove only one container, do "docker1 stop xxxxxx" followed by "docker1 rm xxxxxxx".
Remove the images
If you have created some result files in the /workdir/$USER, these results files are owned by root. You cannot delete or move these files. To get ownership back, run
This command would operate on the whole /workdir/$USER. If this directory is large, it could take a very long time. To claim a directory you can do "docker1 claim path_to_the_directory"
2.6 Run Rscript
So far, you have done interactive shell in Docker, including both R-shell and BASH sehll. If you just want to run a script, e.g. /workdir/$USER/myscript.R, you can do:
docker1 run --rm rocker/r-ver:4.1.1 Rscript /workdir/myscript.R
The "--rm" option is to remove the container after the work is done.
Part 3. Rstudio through Docker
3.1 Start a Docker container R 4.1.1 using the image "rocker/rstudio" built by the Rocker project
3.2 Add your BioHPC user ID into the container
3.3. Open your browser, and point to the Rstudio server you have started. Replace "cbsuxxxxxxx" with your server name, and "yyyy" with your port number.
After you login with your BioHPC user id, change the working directory to "/workdir", so that you can access your data files in "/workdir/$USER".
To access the Linux shell in Container, click the "Terminal" tab. Try to install a Linux software in Terminal
3.4 Commit the change in your container to a new image file
If you install any software/packages in the Docker container, you might want to commit the change into a new Docker image file. Otherwise, if a container is killed, or host machine is restarted, everything in the container is lost. To do that, you need to go back to the host Linux shell, and run the command "docker1 commit". The "xxxxxxx" is the Docker container id.