class: center, middle, inverse, title-slide # Setting up isolated project-libraries using {<code>renv</code>} ## <br> ### Catherine Blatter ### RRTM, 15.03.2022 --- layout: true <div class="footer">Catherine Blatter | Setting up isolated project-libraries using {`renv`} | RRTM, 15.03.2022 </div> --- # Have you ever... -- <br> - written code that produced beautiful output until suddenly it didn't? -- <br> - ran a statistical model that - out of the blue - throws a warning? -- <br> - copied code from a colleague that just does not work on your computer? -- <br> ** <svg viewBox="0 0 448 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M190.5 66.9l22.2-22.2c9.4-9.4 24.6-9.4 33.9 0L441 239c9.4 9.4 9.4 24.6 0 33.9L246.6 467.3c-9.4 9.4-24.6 9.4-33.9 0l-22.2-22.2c-9.5-9.5-9.3-25 .4-34.3L311.4 296H24c-13.3 0-24-10.7-24-24v-32c0-13.3 10.7-24 24-24h287.4L190.9 101.2c-9.8-9.3-10-24.8-.4-34.3z"></path></svg> Time-consuming and frustrating issue** *(typically after updating)* -- <br> - Popular solution: **.mint[*"If there is one thing I have learnt in R is to never ever update anything!"*]** .fn-r[*quote from and with permission of Lauriane*] --- class:huge # Goals of today's research flash <br> > Describe the internal setup and interrelations between R, RStudio, R packages and their versions -- <br> > Present a conceptual solution & technical framework designed to tackle the issue: - Isolated project-libraries with {`renv`} --- # R, RStudio and R packages -- <img src="r-overview.svg" width="60%" style="display: block; margin: auto;" /> --- # R, RStudio and R packages <img src="r-overview-2.svg" width="60%" style="display: block; margin: auto;" /> .fn-r[.reddark[Versions] to report in a publication] --- # R, RStudio and R packages <img src="r-overview-3.svg" width="60%" style="display: block; margin: auto;" /> --- # The R System library -- - Directory.sup[1] where packages are **stored**.sup[2] and **looked up**.sup[3] for an analysis -- .fn-r[.sup[1]use `.libPaths()` to check where, .sup[2]stored = `install.packages()`, .sup[3]looked up = `library()` ] <img src="r-library.svg" width="700px" style="display: block; margin: auto;" /> -- <br> - Installation/update of a package == **removal** of a previous version *e.g. update to* {`ggplot2`} 3.3.5 == <span style="text-decoration:line-through; color: var(--reddark);">2.3.9</span> --- # R package dependencies -- - Packages can be **linked** to other packages (*dependent*): e.g. {`ggplot2`} has got 30 dependencies: <img src='ggplot-dependency-graph.jpg' > .fn-r[*Fig.source:* [RStudio](https://www.rstudio.com/blog/challenges-in-package-management/), February 2022] -- - Installation/update of 1 package == updates **all dependencies** as well --- # What does this mean for an analysis? -- .huge[Example] - Explicit use of 3 packages (by `library()`): ```r library(ggplot2) # powerful plotting library library(lme4) # multi-level modelling library(naniar) # dealing with missing values ``` -- - <svg viewBox="0 0 448 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M190.5 66.9l22.2-22.2c9.4-9.4 24.6-9.4 33.9 0L441 239c9.4 9.4 9.4 24.6 0 33.9L246.6 467.3c-9.4 9.4-24.6 9.4-33.9 0l-22.2-22.2c-9.5-9.5-9.3-25 .4-34.3L311.4 296H24c-13.3 0-24-10.7-24-24v-32c0-13.3 10.7-24 24-24h287.4L190.9 101.2c-9.8-9.3-10-24.8-.4-34.3z"></path></svg> R implicitly uses 41 packages -- .huge[Usual analysis] - Explicit use of 10-15 packages -- - <svg viewBox="0 0 448 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M190.5 66.9l22.2-22.2c9.4-9.4 24.6-9.4 33.9 0L441 239c9.4 9.4 9.4 24.6 0 33.9L246.6 467.3c-9.4 9.4-24.6 9.4-33.9 0l-22.2-22.2c-9.5-9.5-9.3-25 .4-34.3L311.4 296H24c-13.3 0-24-10.7-24-24v-32c0-13.3 10.7-24 24-24h287.4L190.9 101.2c-9.8-9.3-10-24.8-.4-34.3z"></path></svg> Implicit interrelations are difficult to gasp & impossible to track by hand -- - .reddark[*Issue*]: (recommended) update <svg viewBox="0 0 448 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M190.5 66.9l22.2-22.2c9.4-9.4 24.6-9.4 33.9 0L441 239c9.4 9.4 9.4 24.6 0 33.9L246.6 467.3c-9.4 9.4-24.6 9.4-33.9 0l-22.2-22.2c-9.5-9.5-9.3-25 .4-34.3L311.4 296H24c-13.3 0-24-10.7-24-24v-32c0-13.3 10.7-24 24-24h287.4L190.9 101.2c-9.8-9.3-10-24.8-.4-34.3z"></path></svg> unintended consequence --- # The conceptual solution -- <img src="r-library.svg" width="600px" style="display: block; margin: auto;" /> --- # The conceptual solution <img src="r-library-renv.svg" width="600px" style="display: block; margin: auto;" /> > .mintdark[Isolated] project-libraries (*also called environments*) --- # The conceptual solution <img src="r-library-2.svg" width="600px" style="display: block; margin: auto;" /> > .mintdark[Isolated] project-libraries (*also called environments*) --- # Isolated libraries (environments) -- <br> - Standard practice in programming languages, e.g. Python .fn-r[further info: https://environments.rstudio.com/, https://peps.python.org/pep-0405/] -- - ...one of the main reasons that non-programmers do not proceed with Python -- <br> - Fundamentally different to 'usual' interaction with a computer: - Imagine the need to define *how* to use MS Word before creating a new Word-document <br> -- - R user = academic researcher, often without any formal training in programming - lack of knowledge on situation and solution --- # The technical framework: {`renv`} <img src="r-library-2.svg" style="position:absolute; top:30px; right:40px; max-width:20%;"> -- - R package designed to create **.mintdark[*r*]**eproducible **.mintdark[*env*]**ironments by initiating isolated project-libraries <br> .fn-l[Kevin Ushey (2022). renv: Project Environments. R package version 0.15.2.] -- <img src="renv-example.png" width=400 style="float:right"> <br> - What do I see when using {`renv`}? - a subfolder `renv` that contains the isolated library (*useful on your computer*) - a lockfile `renv.lock` where all versions are documented (*useful for creating the same library on another computer*) --- # Reflection & key points -- - {`renv`} is not a uniform solution to computational reproducibility <br> -- - Alternatives are available - require more technical know-how <br> -- <br> - {`renv`} offers a lightweight & manageable solution to - setup and use multiple libraries across your computer - easily document all versions and dependencies of packages - share your environment-setup with other collaborators --- layout:false class: title-slide-final # Thank you for your attention <div style="position: absolute; left: 100px; bottom: 200px;"> <svg viewBox="0 0 496 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3zm44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z"></path></svg> cathblatter </a> <svg viewBox="0 0 512 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <path d="M459.37 151.716c.325 4.548.325 9.097.325 13.645 0 138.72-105.583 298.558-298.558 298.558-59.452 0-114.68-17.219-161.137-47.106 8.447.974 16.568 1.299 25.34 1.299 49.055 0 94.213-16.568 130.274-44.832-46.132-.975-84.792-31.188-98.112-72.772 6.498.974 12.995 1.624 19.818 1.624 9.421 0 18.843-1.3 27.614-3.573-48.081-9.747-84.143-51.98-84.143-102.985v-1.299c13.969 7.797 30.214 12.67 47.431 13.319-28.264-18.843-46.781-51.005-46.781-87.391 0-19.492 5.197-37.36 14.294-52.954 51.655 63.675 129.3 105.258 216.365 109.807-1.624-7.797-2.599-15.918-2.599-24.04 0-57.828 46.782-104.934 104.934-104.934 30.213 0 57.502 12.67 76.67 33.137 23.715-4.548 46.456-13.32 66.599-25.34-7.798 24.366-24.366 44.833-46.132 57.827 21.117-2.273 41.584-8.122 60.426-16.243-14.292 20.791-32.161 39.308-52.628 54.253z"></path></svg> cathblatter</a> <svg viewBox="0 0 512 512" style="height:1em;position:relative;display:inline-block;top:.1em;" xmlns="http://www.w3.org/2000/svg"> <g label="icon" id="layer6" groupmode="layer"> <path id="path2" d="m 336.6206,194.53756 c -7.12991,-3.32734 -13.8671,-5.55949 -20.25334,-6.61343 -6.36534,-1.09517 -16.57451,-1.61223 -30.71059,-1.61223 h -36.70409 v 152.74712 h 37.63425 c 14.6735,0 26.08126,-1.01267 34.22385,-3.01709 8.14259,-2.00442 14.92159,-4.52592 20.35674,-7.62608 5.43519,-3.07925 10.416,-6.8615 14.94192,-11.38742 14.4876,-14.71475 21.74129,-33.27334 21.74129,-55.7176 0,-22.05151 -7.44016,-40.05177 -22.34085,-53.98159 -5.49732,-5.16674 -11.82143,-9.44459 -18.88918,-12.79281 z M 255.99999,8.0000031 C 119.02153,8.0000031 8.0000034,119.04185 8.0000034,255.99998 8.0000034,392.95812 119.02153,504 255.99999,504 392.97849,504 504,392.95812 504,255.99998 504,119.04185 392.97849,8.0000031 255.99999,8.0000031 Z M 173.66372,365.51268 H 144.27546 V 160.1481 h 29.38826 z M 158.94954,138.69619 c -11.13935,0 -20.21208,-9.01056 -20.21208,-20.21208 0,-11.11841 9.05183,-20.191181 20.21208,-20.191181 11.18058,0 20.23244,9.051831 20.23244,20.191181 -0.0219,11.22184 -9.05186,20.21208 -20.23244,20.21208 z m 241.3866,163.59715 c -5.29051,12.54475 -12.83407,23.58066 -22.65053,33.08742 -9.98203,9.83734 -21.59659,17.19443 -34.84378,22.19616 -7.74983,3.01709 -14.83852,5.06335 -21.30725,6.11726 -6.4891,1.01267 -18.82759,1.50883 -37.07593,1.50883 H 219.5033 V 160.1481 h 69.23318 c 27.96195,0 50.03378,4.1541 66.31951,12.54476 16.26485,8.36977 29.18144,20.72859 38.79164,36.97254 9.61013,16.26483 14.4254,34.01757 14.4254,53.19607 0.0227,13.76426 -2.66619,26.90802 -7.93576,39.43187 z" style="stroke-width:0.07717"></path> </g></svg> https://orcid.org/0000-0002-4069-7178</a> <div> --- # Setup {`renv`} Official documentation: https://rstudio.github.io/renv/articles/renv.html 1) Open the `.Rproj`-file of the desired analysis-folder 2) Call `install.packages("renv")` 3) Call `renv::init()` > *this creates the subfolder and the `renv.lock`-file * > {`renv`} interacts with you in the console & prints what it does 4) Continue working as usual - if you need to install further packages, they are installed in the project-library 5) From time to time: call `renv::snapshot()` > *adds all the packages used in this project to the `renv.lock`-file * .fn-l[Setup tested with {`renv`} 0.15.2, R Version 4.1.3 on Mac OS X 12.2.1] --- # Alternatives <br> - https://annotater.liomys.mx/ - annotates the version or R packages used in a script (*downside:* no dependencies, manual installation) <br> - https://github.com/MilesMcBain/capsule - mimics the lockfile, not the isolated library (*downside*: need to set-up the environment every time you work on the analysis) <br> - Setting up everything by hand with {`base`}-R --- # Why {`renv`} ? -- <img src="r-library-why.svg" style="position:absolute; right:350px;">