From 121f72d872630903371a9c568fc095263dbc2161 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Rochette?= Date: Fri, 30 Oct 2020 18:35:56 +0100 Subject: [PATCH] New renv instructions --- DESCRIPTION | 2 +- README.Rmd | 49 ++++++++++++++++++++++- README.md | 54 ++++++++++++++++++++++++- inst/renv/renv_instructions.Rmd | 70 ++++++++++++++++++++++++++++----- 4 files changed, 160 insertions(+), 15 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 8bf7571..0cf4f8a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -25,4 +25,4 @@ VignetteBuilder: Encoding: UTF-8 LazyData: true Roxygen: list(markdown = TRUE) -RoxygenNote: 7.1.0 +RoxygenNote: 7.1.1 diff --git a/README.Rmd b/README.Rmd index 73c0080..fdddf07 100644 --- a/README.Rmd +++ b/README.Rmd @@ -14,7 +14,7 @@ knitr::opts_chunk$set( ) ``` -# devindocker +# {devindocker} [![R build status](https://github.com/ThinkR-open/devindocker/workflows/R-CMD-check/badge.svg)](https://github.com/ThinkR-open/devindocker/actions) @@ -48,7 +48,10 @@ dir.create(project_path) cat("# my R file", file = file.path(project_path, "my-file.R")) ``` +### Work in a Docker environment + Launch a Docker container with your directory inside. This should be a container with Rstudio server inside. +_Note that packages you install will not be kept after you stop the container, but RStudio preferences will_ ```{r} # Which path to your working directory / project project_path <- file.path(tempdir, "myproject") @@ -70,7 +73,49 @@ launch_proj_docker( port = port) ``` -When you're done, do not forget to stop properly the Rstudio Server: Click on Top right button to quit or `q()` in the console. +When you're done, do not forget to stop properly the Rstudio Server: Click on Top right button to quit or `q()` in the console. + +Then, end the container. +```{r} +# Stop Docker properly +stop_proj_docker(project_path = project_path) +``` + +### Use {renv} inside Docker and keep installation of packages + +Launch a Docker container with your directory inside. This should be a container with Rstudio server inside. +_Note that packages you install will be kept after you stop the container, as well as RStudio preferences._ +```{r} +# Which path to your working directory / project +project_path <- file.path(tempdir, "myproject") + +# Which container (with Rstudio inside) ? ---- +# https://hub.docker.com/r/thinkr/rstudio3_5_2_geo +# https://hub.docker.com/r/rocker/verse +container <- c("thinkr/rstudio3_5_2_geo", + "rocker/verse")[1] + +# Which port ? ---- +# _Useful if multiple Rstudio Server to launch +port <- 8788 + +# My renv cache directory on my local computer +# Used as persistent drive for all you Docker container with {devindocker} +renv_cache <- "~/renv_cache" + +# Start Docker project ---- +devindocker::launch_proj_docker( + project_path = project_path, + container = container, + port = port, + renv_cache = renv_cache, + renv_inst = TRUE, # Add an Rmd with instructions inside your project + update_docker = TRUE +) + +``` + +When you're done, do not forget to stop properly the Rstudio Server: Click on Top right button to quit or `q()` in the console. Then, end the container. ```{r} diff --git a/README.md b/README.md index 2065f8d..23d2a37 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ -# devindocker +# {devindocker} @@ -43,8 +43,12 @@ dir.create(project_path) cat("# my R file", file = file.path(project_path, "my-file.R")) ``` +### Work in a Docker environment + Launch a Docker container with your directory inside. This should be a -container with Rstudio server inside. +container with Rstudio server inside. +*Note that packages you install will not be kept after you stop the +container, but RStudio preferences will* ``` r # Which path to your working directory / project @@ -76,3 +80,49 @@ Then, end the container. # Stop Docker properly stop_proj_docker(project_path = project_path) ``` + +### Use {renv} inside Docker and keep installation of packages + +Launch a Docker container with your directory inside. This should be a +container with Rstudio server inside. +*Note that packages you install will be kept after you stop the +container, as well as RStudio preferences.* + +``` r +# Which path to your working directory / project +project_path <- file.path(tempdir, "myproject") + +# Which container (with Rstudio inside) ? ---- +# https://hub.docker.com/r/thinkr/rstudio3_5_2_geo +# https://hub.docker.com/r/rocker/verse +container <- c("thinkr/rstudio3_5_2_geo", + "rocker/verse")[1] + +# Which port ? ---- +# _Useful if multiple Rstudio Server to launch +port <- 8788 + +# My renv cache directory on my local computer +# Used as persistent drive for all you Docker container with {devindocker} +renv_cache <- "~/renv_cache" + +# Start Docker project ---- +devindocker::launch_proj_docker( + project_path = project_path, + container = container, + port = port, + renv_cache = renv_cache, + renv_inst = TRUE, # Add an Rmd with instructions inside your project + update_docker = TRUE +) +``` + +When you’re done, do not forget to stop properly the Rstudio Server: +Click on Top right button to quit or `q()` in the console. + +Then, end the container. + +``` r +# Stop Docker properly +stop_proj_docker(project_path = project_path) +``` diff --git a/inst/renv/renv_instructions.Rmd b/inst/renv/renv_instructions.Rmd index 7624173..65d1200 100644 --- a/inst/renv/renv_instructions.Rmd +++ b/inst/renv/renv_instructions.Rmd @@ -1,5 +1,5 @@ --- -title: "Use renv" +title: "Use renv inside Docker container for package development" --- ```{r setup, include=FALSE} @@ -55,14 +55,6 @@ if (dir.exists("/opt/local/renv/cache")) { # Cache inside the docker container with persistent drive Sys.setenv(RENV_PATHS_CACHE = "/opt/local/renv/cache") renv::settings$use.cache(TRUE) -} else if (dir.exists("/mnt/Data/renv_cache")) { - # Your local path on linux/unix if project used out of Docker - Sys.setenv(RENV_PATHS_CACHE = "/mnt/Data/renv_cache") - renv::settings$use.cache(TRUE) -} else if (dir.exists("D:/renv_cache_windows/")) { - # Your local path on Windows if project used out of Docker - Sys.setenv(RENV_PATHS_CACHE = "D:/renv_cache_windows") - renv::settings$use.cache(TRUE) } else { # No cache renv::settings$use.cache(FALSE) @@ -76,6 +68,27 @@ If you are satisfied with the effects, run: renv::snapshot() ``` +Note that you may want to limit the number of packages added in the lockfile to the one listed in your DESCRIPTION file. To do so, you can use: +```{r} +renv::snapshot(type = "explicit") +``` +But in you are in the process of package development, you may want to also share versions of packages like {devtools}, {roxygen2}, {usethis}, {testthat}, {covr}, {attachment}, ... +As well as {renv} itself. +In this case, you can use the `package` parameter : +```{r} +# Update DESCRIPTION file +attachment::att_amend_desc() +# List dependencies +custom_packages <- c(attachment::att_from_description(), + "renv", + "devtools", "roxygen2", "usethis", + "testthat", "covr", "attachment", + "pkgdown") +# Snapshot +renv::snapshot(packages = custom_packages) +``` + + If you are not satisfied, run: ```{r} renv::restore() # instead of snapshot() @@ -86,6 +99,16 @@ And after you ran `devtools::check()`, store your packages list with: ```{r} renv::snapshot() +# or only those in DESCRIPTION with +renv::snapshot(type = "explicit") +# OR packages in DESCRIPTION + development utilities +attachment::att_amend_desc() +custom_packages <- c(attachment::att_from_description(), + "renv", + "devtools", "roxygen2", "usethis", + "testthat", "covr", "attachment", + "pkgdown") +renv::snapshot(packages = custom_packages) ``` ## If you updated your branch from the server @@ -94,13 +117,40 @@ There may be new packages needed, therefore run: renv::restore() ``` -# If you need to upgrade (or otherwise change) the version of renv +# If you need to upgrade or downgrade + +## Upgrade (or otherwise change) the version of renv This should normally be automatically done in the `renv/activate.R`, but you can do it with: ```{r} renv::upgrade() ``` +## Upgrade or fix CRAN repository +```{r} +lock <- renv:::lockfile("renv.lock") + +# set the repositories for a lockfile +# All CRAN : "https://cloud.r-project.org" +# Fixed MRAN example: "https://mran.revolutionanalytics.com/snapshot/2019-08-05" +# Fixed CRAN date example, allowing for Linux binaries: "https://packagemanager.rstudio.com/all/308" +# Latest CRAN repository allowing for Linux binaries: "https://packagemanager.rstudio.com/all/latest" +lock$repos(CRAN = "https://packagemanager.rstudio.com/all/latest") + +# write to file +lock$write("renv.lock") + +# Update your packages +update.packages() +``` + +## Downgrade a package to a specific version +Depending on the repos you chose, you may not be able to choose a specific package version. +Instead use: +```{r} +remotes::install_version("golem", version = "0.3.0", repos = "https://cloud.r-project.org") +``` + # Use git inside the Docker container ## First time