Initialize and Snapshot

The following examples will guide you through the process of initializing a project with {renv} and then snapshotting the project to capture the packages used in the project. It is recommended that you fork the intro-to-renv-exercises repository to your GitHub account and clone it to your local machine to follow along with the examples. This will allow the course facilitators to see your work, provide feedback, and troubleshoot with you as needed.

However, this is not strictly necessary. You can also follow along by creating a new project on your local machine and initializing {renv} in that project. If you choose to do this, you can skip the forking and cloning steps and instead download a zip file of the repository.

Getting Started

The exercises for initiating and snapshotting a project can be found in the init-snapshot-exercises folder of the intro-to-renv-exercises repo.

Instructions

  1. Open the R project, init-snapshot-exercises.Rproj.
  2. Install the {renv} package if needed.
  3. Initialize a new project with {renv} with renv::init().
  4. Create a new R script in this folder, init-snapshot-exercises, called script.R.
  5. Write code that uses a variety of different packages.
    • Try experimenting with calls to library() and namespacing functions e.g. package::function(). If you’re short on time, there’s some example code included below.
  6. Save the script.
  7. Run the renv::dependencies() function to see the new dependencies of your project.
    • What packages are listed? Is this what you expected?
  8. Check the status of your project with renv::status().
    • What do you notice about the results printed to the console?
  9. Save these new dependencies to the renv.lock file with renv::snapshot().
  10. (Optional) Commit your changes to your repository, and push them to GitHub.
    • This should include a new .Rprofile file, the renv.lock file, and some components of the renv folder (renv/.gitignore, renv/activate.R, renv/settings.json).
  11. Close the R project.

Example Code

library(ggplot2)
library(dplyr)

# Read the iris dataset
data(iris)

# Print the first 6 rows
head(iris)

# Create a scatter plot of Sepal.Length and Sepal.Width
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +
  geom_point()
  
# Perform some data analysis using dplyr
iris %>%
  group_by(Species) %>%
  summarise(mean_sepal_length = mean(Sepal.Length))
  
# Create a linear mixed effects model with {lme4}
lme4::lmer(Sepal.Length ~ Sepal.Width + (1 | Species), data = iris)
Back to top