Code
::init() renv
Tony D
March 11, 2025
A guide to using the renv package in R for creating reproducible environments and managing package versions. This document covers initialization, package installation, lock file management, and Python integration.
This document provides a comprehensive guide to using the renv
package in R for creating reproducible environments and managing package versions. It covers the entire workflow, from initializing renv
in a project to installing and updating packages, managing the lock file, and even integrating renv
with Python. This guide is also part of the R handbook, making it a valuable resource for anyone looking to improve the reproducibility of their R projects.
Explains how to use the renv
package in R for reproducible environments and package version management. Covers renv
initialization, package installation/updates, lock file management, and Python integration.
The renv package helps you create reproducible environments for your R projects
It section also update into R handbook
# renv for R
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
---
title: "Version control with renv"
author: "Tony D"
execute:
warning: false
error: false
eval: false
date: "2025-03-11"
categories:
- Tool
- R
- Python
image: "images.png"
---
A guide to using the renv package in R for creating reproducible environments and managing package versions. This document covers initialization, package installation, lock file management, and Python integration.
This document provides a comprehensive guide to using the `renv` package in R for creating reproducible environments and managing package versions. It covers the entire workflow, from initializing `renv` in a project to installing and updating packages, managing the lock file, and even integrating `renv` with Python. This guide is also part of the [R handbook](https://jcfly3000.github.io/Into-R/intro/1%20basic%20R.html#version-control), making it a valuable resource for anyone looking to improve the reproducibility of their R projects.
Explains how to use the `renv` package in R for reproducible environments and package version management. Covers `renv` initialization, package installation/updates, lock file management, and Python integration.
The renv package helps you create reproducible environments for your R projects
It section also update into [R handbook](https://jcfly3000.github.io/Into-R/intro/1%20basic%20R.html#version-control)

# renv for R
## inital renv and create renv.lock with current loaded pacakge
```{r}
renv::init()
```
## show all installed pacakge
```{r}
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
```{r}
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
```{r}
# it will not work
# library(lubridate)
```
## need to install new package with renv::install
```{r}
renv::install('lubridate')
```
```{r}
library(lubridate)
```
## check current package and renv package
```{r}
renv::status()
```
## update lock file
```{r}
renv::snapshot()
```
## check status again
```{r}
renv::status()
```
## make all current pakcage version back to renv list
```{r}
renv::restore()
```
## update all pakcage in renv. recommand do it once a year to keep package updated.
```{r}
renv::update()
```
## update renv itself only
```{r}
renv::upgrade()
```
## close renv in a project
```{r}
renv::deactivate()
```
## re enable renv in a project
```{r}
renv::activate()
```
# renv in python
## set python version
```{r}
renv::use_python()
```
## check python version in renv
```{python}
from sys import version as python_formatted_version
print(python_formatted_version)
```
## list all installed pacakge in python
```{python}
import os
print(os.system('pip freeze'))
```
## install package
```{python}
import os
os.system('python3.10 -m pip install siuba')
```
## update lock file
```{r}
renv::snapshot()
```
## uninstall package
```{python}
import os
os.system('yes | python3.10 -m pip uninstall siuba')
```
## make all current pakcage version back to renv list
```{r}
renv::restore()
```
## install package
```{python}
import os
os.system('python3.10 -m pip install requests')
```
## update lock file
```{r}
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