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

renv::init()

show all installed pacakge

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

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

# it will not work
# library(lubridate)

need to install new package with renv::install

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

check current package and renv package

renv::status()

update lock file

renv::snapshot()

check status again

renv::status()

make all current pakcage version back to renv list

renv::restore()

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

renv::update()

update renv itself only

renv::upgrade()

close renv in a project

renv::deactivate()

re enable renv in a project

renv::activate()

renv in python

set python version

renv::use_python()

check python version in renv

from sys import version as python_formatted_version
print(python_formatted_version)

list all installed pacakge in python

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

install package

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

update lock file

renv::snapshot()

uninstall package

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

make all current pakcage version back to renv list

renv::restore()

install package

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

update lock file

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