Version control with renv

Tool
R
Python
Author

Tony D

Published

March 11, 2025

The renv package helps you create reproducible environments for your R projects

It section also update into R handbook

# renv for R

inital renv and create renv.lock with current loaded pacakge

Code
renv::init()

show all installed pacakge

Code
installed_pacakge = as.data.frame(installed.packages()[,c(1,3:4)])
installed_pacakge = installed_pacakge[is.na(installed_pacakge$Priority),1:2,drop=FALSE]
installed_pacakge

show all installed pacakge and uploaded pacakge

Code
library(dplyr)
installed_pacakge = as.data.frame(installed.packages()[,c(1,3:4)])
installed_pacakge = installed_pacakge[is.na(installed_pacakge$Priority),1:2,drop=FALSE]
installed_pacakge |> filter(Package %in% (.packages()))

when using renv and install new pakcage

Code
# it will not work
# library(lubridate)

need to install new package with renv::install

Code
renv::install('lubridate')
Code
library(lubridate)

check current package and renv package

Code
renv::status()

update lock file

Code
renv::snapshot()

check status again

Code
renv::status()

make all current pakcage version back to renv list

Code
renv::restore()

update all pakcage in renv. recommand do it once a year to keep package updated.

Code
renv::update()

update renv itself only

Code
renv::upgrade()

close renv in a project

Code
renv::deactivate()

re enable renv in a project

Code
renv::activate()

renv in python

set python version

Code
renv::use_python()

check python version in renv

Code
from sys import version as python_formatted_version
print(python_formatted_version)

list all installed pacakge in python

Code
import os
print(os.system('pip freeze'))

install package

Code
import os
os.system('python3.10 -m pip install siuba')

update lock file

Code
renv::snapshot()

uninstall package

Code
import os
os.system('yes | python3.10 -m pip uninstall siuba')

make all current pakcage version back to renv list

Code
renv::restore()

install package

Code
import os
os.system('python3.10 -m pip install requests')

update lock file

Code
renv::snapshot()

reference:

Robust R Code That Will Work Forever With {renv} by Albert Rapp:

https://www.youtube.com/watch?v=Oen9xhEh8PY

https://rstudio.github.io/renv/articles/renv.html