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
- Open the R project,
init-snapshot-exercises.Rproj
. - Install the {renv} package if needed.
- Initialize a new project with {renv} with
renv::init()
. - Create a new R script in this folder,
init-snapshot-exercises
, calledscript.R
. - 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.
- Try experimenting with calls to
- Save the script.
- Run the
renv::dependencies()
function to see the new dependencies of your project.- What packages are listed? Is this what you expected?
- Check the status of your project with
renv::status()
.- What do you notice about the results printed to the console?
- Save these new dependencies to the
renv.lock
file withrenv::snapshot()
. - (Optional) Commit your changes to your repository, and push them to GitHub.
- This should include a new
.Rprofile
file, therenv.lock
file, and some components of therenv
folder (renv/.gitignore
,renv/activate.R
,renv/settings.json
).
- This should include a new
- 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}
::lmer(Sepal.Length ~ Sepal.Width + (1 | Species), data = iris) lme4