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:
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:
Expand to learn about dealing with identical function names from different packages
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:
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
Expand to learn about managing package versions
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.3.1 (2023-06-16)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 22.04.3 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.3.6 ggplot2_3.4.2
loaded via a namespace (and not attached):
[1] vctrs_0.6.3 nlme_3.1-162 cli_3.6.1 knitr_1.43
[5] rlang_1.1.1 xfun_0.39 generics_0.1.3 renv_1.0.0
[9] jsonlite_1.8.7 glue_1.6.2 colorspace_2.1-0 htmltools_0.5.5
[13] scales_1.2.1 fansi_1.0.4 rmarkdown_2.23 grid_4.3.1
[17] evaluate_0.21 munsell_0.5.0 tibble_3.2.1 fastmap_1.1.1
[21] yaml_2.3.7 lifecycle_1.0.3 compiler_4.3.1 dplyr_1.1.2
[25] pkgconfig_2.0.3 rstudioapi_0.15.0 lattice_0.21-8 digest_0.6.32
[29] R6_2.5.1 tidyselect_1.2.0 utf8_1.2.3 parallel_4.3.1
[33] mnormt_2.1.1 pillar_1.9.0 magrittr_2.0.3 withr_2.5.0
[37] tools_4.3.1 gtable_0.3.3
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.