+ - 0:00:00
Notes for current slide
Notes for next slide

Setting up isolated project-libraries using {renv}


Catherine Blatter

RRTM, 15.03.2022

1/19

Have you ever...

2/19

Have you ever...


  • written code that produced beautiful output until suddenly it didn't?
2/19

Have you ever...


  • written code that produced beautiful output until suddenly it didn't?


  • ran a statistical model that - out of the blue - throws a warning?
2/19

Have you ever...


  • written code that produced beautiful output until suddenly it didn't?


  • ran a statistical model that - out of the blue - throws a warning?


  • copied code from a colleague that just does not work on your computer?
2/19

Have you ever...


  • written code that produced beautiful output until suddenly it didn't?


  • ran a statistical model that - out of the blue - throws a warning?


  • copied code from a colleague that just does not work on your computer?


Time-consuming and frustrating issue (typically after updating)

2/19

Have you ever...


  • written code that produced beautiful output until suddenly it didn't?


  • ran a statistical model that - out of the blue - throws a warning?


  • copied code from a colleague that just does not work on your computer?


Time-consuming and frustrating issue (typically after updating)


  • Popular solution: "If there is one thing I have learnt in R is to never ever update anything!"

quote from and with permission of Lauriane

2/19

Goals of today's research flash


Describe the internal setup and interrelations between R, RStudio, R packages and their versions

3/19

Goals of today's research flash


Describe the internal setup and interrelations between R, RStudio, R packages and their versions


Present a conceptual solution & technical framework designed to tackle the issue:

  • Isolated project-libraries with {renv}
3/19

R, RStudio and R packages

4/19

R, RStudio and R packages

4/19

R, RStudio and R packages

Versions to report in a publication

5/19

R, RStudio and R packages

6/19

The R System library

7/19

The R System library

  • Directory1 where packages are stored2 and looked up3 for an analysis
7/19

The R System library

  • Directory1 where packages are stored2 and looked up3 for an analysis

1use .libPaths() to check where, 2stored = install.packages(), 3looked up = library()

7/19

The R System library

  • Directory1 where packages are stored2 and looked up3 for an analysis

1use .libPaths() to check where, 2stored = install.packages(), 3looked up = library()


  • Installation/update of a package == removal of a previous version
    e.g. update to {ggplot2} 3.3.5 == 2.3.9
7/19

R package dependencies

8/19

R package dependencies

  • Packages can be linked to other packages (dependent): e.g. {ggplot2} has got 30 dependencies:

Fig.source: RStudio, February 2022

8/19

R package dependencies

  • Packages can be linked to other packages (dependent): e.g. {ggplot2} has got 30 dependencies:

Fig.source: RStudio, February 2022

  • Installation/update of 1 package == updates all dependencies as well
8/19

What does this mean for an analysis?

9/19

What does this mean for an analysis?

Example

  • Explicit use of 3 packages (by library()):
library(ggplot2) # powerful plotting library
library(lme4) # multi-level modelling
library(naniar) # dealing with missing values
9/19

What does this mean for an analysis?

Example

  • Explicit use of 3 packages (by library()):
library(ggplot2) # powerful plotting library
library(lme4) # multi-level modelling
library(naniar) # dealing with missing values
  • R implicitly uses 41 packages
9/19

What does this mean for an analysis?

Example

  • Explicit use of 3 packages (by library()):
library(ggplot2) # powerful plotting library
library(lme4) # multi-level modelling
library(naniar) # dealing with missing values
  • R implicitly uses 41 packages

Usual analysis

  • Explicit use of 10-15 packages
9/19

What does this mean for an analysis?

Example

  • Explicit use of 3 packages (by library()):
library(ggplot2) # powerful plotting library
library(lme4) # multi-level modelling
library(naniar) # dealing with missing values
  • R implicitly uses 41 packages

Usual analysis

  • Explicit use of 10-15 packages

  • Implicit interrelations are difficult to gasp & impossible to track by hand

9/19

What does this mean for an analysis?

Example

  • Explicit use of 3 packages (by library()):
library(ggplot2) # powerful plotting library
library(lme4) # multi-level modelling
library(naniar) # dealing with missing values
  • R implicitly uses 41 packages

Usual analysis

  • Explicit use of 10-15 packages

  • Implicit interrelations are difficult to gasp & impossible to track by hand

  • Issue: (recommended) update unintended consequence

9/19

The conceptual solution

10/19

The conceptual solution

10/19

The conceptual solution

Isolated project-libraries (also called environments)

11/19

The conceptual solution

Isolated project-libraries (also called environments)

12/19

Isolated libraries (environments)

13/19

Isolated libraries (environments)


  • Standard practice in programming languages, e.g. Python

further info: https://environments.rstudio.com/, https://peps.python.org/pep-0405/

13/19

Isolated libraries (environments)


  • Standard practice in programming languages, e.g. Python

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
13/19

Isolated libraries (environments)


  • Standard practice in programming languages, e.g. Python

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


  • Fundamentally different to 'usual' interaction with a computer:

    • Imagine the need to define how to use MS Word before creating a new Word-document


13/19

Isolated libraries (environments)


  • Standard practice in programming languages, e.g. Python

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


  • Fundamentally different to 'usual' interaction with a computer:

    • Imagine the need to define how to use MS Word before creating a new Word-document


  • R user = academic researcher, often without any formal training in programming -
    lack of knowledge on situation and solution
13/19

The technical framework: {renv}

14/19

The technical framework: {renv}

  • R package designed to create reproducible environments
    by initiating isolated project-libraries


Kevin Ushey (2022). renv: Project Environments. R package version 0.15.2.

14/19

The technical framework: {renv}

  • R package designed to create reproducible environments
    by initiating isolated project-libraries


Kevin Ushey (2022). renv: Project Environments. R package version 0.15.2.


  • 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)

14/19

Reflection & key points

15/19

Reflection & key points

  • {renv} is not a uniform solution to computational reproducibility

15/19

Reflection & key points

  • {renv} is not a uniform solution to computational reproducibility

  • Alternatives are available - require more technical know-how

15/19

Reflection & key points

  • {renv} is not a uniform solution to computational reproducibility

  • Alternatives are available - require more technical know-how

  • {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

15/19

Thank you for your attention

cathblatter

cathblatter

https://orcid.org/0000-0002-4069-7178

16/19

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

Setup tested with {renv} 0.15.2, R Version 4.1.3 on Mac OS X 12.2.1

17/19

Alternatives




  • Setting up everything by hand with {base}-R
18/19

Why {renv} ?

19/19

Why {renv} ?

19/19

Have you ever...

2/19
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow