This is my first time to use renv
package. I want it can help me to keep the enivronment clean and robust.
library(renv)
When I run the command, which prompt me that:
Welcome to renv!It looks like this is your first time using renv. This is a one-time message,
briefly describing some of renv's functionality.renv maintains a local cache of data on the filesystem, located at:
- "C:/Users/zl/AppData/Local/renv"
This path can be customized: please see the documentation in ?renv::paths
.
renv will also write to files within the active project folder, including:
- A folder 'renv' in the project directory, and
- A lockfile called 'renv.lock' in the project directory.
In particular, projects using renv will normally use a private, per-project
R library, in which new packages will be installed. This project library is
isolated from other R libraries on your system.
In addition, renv will update files within your project directory, including:
- .gitignore
- .Rbuildignore
- .Rprofile
Please read the introduction vignette with vignette("renv")
for more information.
You can browse the package documentation online at https://rstudio.github.io/renv/.
The tip tell me some useful information:
-
renv
maintains a local cache, which isC:/Users/zl/AppData/Local/renv
. If you want to check it, you can use therenv::paths
to read it. - At current project,
renv
will use two files to monitor the environment. One isrenv
folder, another isrenv.lock
file. -
renv
provides a projected-based and isolated R package library. - as newber, please read the file of
vignette("renv")
.Rbuildignore
lets you remove arbitrary files and directories from the top level of package..Rprofile
files are user-controllable files to set options and environment variables.
Do you want to proceed? [y/N]: y * "C:/Users/zl/AppData/Local/renv" has been created. * Initializing project ... * Discovering package dependencies ... Done! * Copying packages into the cache ... Done! The following package(s) will be updated in the lockfile:CRAN ===============================
- renv [* -> 0.12.2]
- Lockfile written to 'E:/workform/code/R/renv_learning/renv.lock'.
Restarting R session...
- Project 'E:/workform/code/R/renv_learning' loaded. [renv 0.12.2]
I just open the path to varify its avalibility.C:\Users\zl\AppData\Local\renv
it works. And it also creates a renv.lock
.
All files are listed like that:
linux shell file to get the files in the folder $ tree -L 2 . ├── renv │ ├── activate.R │ ├── library │ └── settings.dcf ├── renv.lock ├── renv_learn1.R └── renv_learning.Rproj2 directories, 5 files
From the above, we can see 2 directories (renv and library) and 5 files.
I run the vignette("renv")
command. I find it is less outdated compared with the online version. Therefore, I continue to use the newest version.
renv::snapshot()
to save the state into the renv.lock
file.
renv::restore()
to revert to the previous state.
The main commands are that:
-
renv::init
, attempts to ensure the newly-created project library. renv::snapshot
renv::restore
The workflow is that:
-
renv::init
allrenv::dependencies()
crawls R files within project. The discovered R pacakges are then installed into the project library with therenv::hydrate()
function. For saving time, therenv:hydrate
will directly copy the appropriate package from the user library to the project library. -
renv::init()
will create a project-based.Rprofile
to write out the necessary infrasture.
to repreduce your project. renv
uses renv::snapshot()
and renv::restore()
to save and restore the state of the project library.
renv
is not a panacea for reproducibility. renv
just remember the version of R packages and provides tools for re-installing.
There are still a number of factors that affects wheather this project.
- the components of the system. Keeping a stable system image is good way to solve it.
vignette("docker", package = "renv")
can help you deal with the combination ofdocker
andrenv
. - The R packages may no longer be avalible.
renv::equip()
can help download external software commonly used when compiling Rfrom sources.
Renv has several important files:
-
.Rprofile
activerenv
for new R sessions -
renv.lock
describe the state of project's library. -
renv/active.R
run by the project. -
renv/library
is the private project library.
For collaboration, .Rprofile
, renv.lock
, renv/active.R
should be committed to your version control system. renv/library
should normally be ignored. renv::init()
will attempt to write the statement in the project .gitignore
file.
In China, sometimes we need to costom and local R package repositories. We can use repos
to complete this.
repos<-c(CRAN = "https://cloud.r-project.org", WORK = "https://work.example.org")
options(repos = repos)
to tell the renv
to work with both the official version and personal repository.
Reference: