Packages

1

Packages are extensions to the base R you get by default. Most times they provide many new functions, bundled around a specific use case. When working with R you will have to rely heavily on packages developed by others. There are many, many great packages out there facilitating your work with R and making possible different analyses, visualizations and much more.

We can compare them somewhat to apps in an appstore:

2 First, we have to install the package: install.packages("packagename"). It’s like downloading an app from the playstore.

3 Then, we have to load them in each session where we want to use them (like tapping on the app icon): library(packagename).

Tip

Note that in install.packages("packagename") we have to put the package into quotation marks, but not when calling library(packagename).

Install packages

Before we can use packages we have to install them once. Most of the openly available packages lie on CRAN (Comprehensive R Archive Network):

install.packages("aRtsy")

But you can also download packages form other sources, for example GitHub. To install a package from there, use:

devtools::install_github("cutterkom/generativeart")

Load packages

After installing a package, we have to load it into our R session. Optimally we do this for all packages we are going to use at the top of our script, so dependencies are easy to spot.

library(aRtsy)

Now we are free to use it, in this case to generate some artwork:

set.seed(1)
canvas_collatz(colors = colorPalette("lava"))

Function conflicts

Of course it can happen that different packages include functions with the same name. For example, look at the message we get when installing and then loading the packages ggplot2 and psych:

install.packages("ggplot2")
install.packages("psych")
library(ggplot2)
library(psych)

Attaching package: 'psych'
The following objects are masked from 'package:ggplot2':

    %+%, alpha

There seems to be a function named alpha which is included in both packages (it just has the same name). The message notifies us that the object alpha from ggplot2 is masked, so if we call it, the alpha function from the psych package is used. To solve that quandary, we can use the :: operator. In front we write the package name, and behind it the name of the function we want to use from that package:

ggplot2::alpha()
psych::alpha()

Deliberate package management

Finally, a quick note on package management and reproducability of your code. R versions and package versions will change over time, in which case also the output of your code might change. Therefore, it is good practice to save the R version and package versions, so your code stays (kind of) reproducable for a longer period of time. The most straight forward thing to do is to just write down your R-version and the package versions at the top of your script. Call the versions you use with:

sessionInfo()
R version 4.4.1 (2024-06-14)
Platform: x86_64-pc-linux-gnu
Running under: Ubuntu 22.04.4 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 
LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.20.so;  LAPACK version 3.10.0

locale:
 [1] LC_CTYPE=C.UTF-8       LC_NUMERIC=C           LC_TIME=C.UTF-8       
 [4] LC_COLLATE=C.UTF-8     LC_MONETARY=C.UTF-8    LC_MESSAGES=C.UTF-8   
 [7] LC_PAPER=C.UTF-8       LC_NAME=C              LC_ADDRESS=C          
[10] LC_TELEPHONE=C         LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C   

time zone: UTC
tzcode source: system (glibc)

attached base packages:
[1] stats     graphics  grDevices datasets  utils     methods   base     

other attached packages:
[1] psych_2.4.3   ggplot2_3.5.1

loaded via a namespace (and not attached):
 [1] vctrs_0.6.5       nlme_3.1-164      cli_3.6.2         knitr_1.46       
 [5] rlang_1.1.3       xfun_0.43         generics_0.1.3    renv_1.0.7       
 [9] jsonlite_1.8.8    glue_1.7.0        colorspace_2.1-0  htmltools_0.5.8.1
[13] scales_1.3.0      fansi_1.0.6       rmarkdown_2.26    grid_4.4.1       
[17] evaluate_0.23     munsell_0.5.1     tibble_3.2.1      fastmap_1.1.1    
[21] yaml_2.3.8        lifecycle_1.0.4   compiler_4.4.1    dplyr_1.1.4      
[25] pkgconfig_2.0.3   lattice_0.22-6    digest_0.6.35     R6_2.5.1         
[29] tidyselect_1.2.1  utf8_1.2.4        parallel_4.4.1    mnormt_2.1.1     
[33] pillar_1.9.0      magrittr_2.0.3    withr_3.0.0       tools_4.4.1      
[37] gtable_0.3.5     

A more elegant approach to manage your packages is to use a dedicated package like renv, which will make it a lot easier to manage your package versions. But this is past the scope of this workshop, just keep in mind it might be something rewarding to look at, if you should start to programm more with R.

Footnotes

  1. Image by Jan Antonin Kolar on Unsplash.↩︎

  2. Icons from icons8.de.↩︎

  3. Icons from icons8.de.↩︎