Skip to content

Commit

Permalink
New renv instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
statnmap committed Oct 30, 2020
1 parent bd0b7a5 commit 121f72d
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 15 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ VignetteBuilder:
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.0
RoxygenNote: 7.1.1
49 changes: 47 additions & 2 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ knitr::opts_chunk$set(
)
```

# devindocker
# {devindocker}

<!-- badges: start -->
[![R build status](https://github.com/ThinkR-open/devindocker/workflows/R-CMD-check/badge.svg)](https://github.com/ThinkR-open/devindocker/actions)
Expand Down Expand Up @@ -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")
Expand All @@ -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}
Expand Down
54 changes: 52 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

<!-- README.md is generated from README.Rmd. Please edit that file -->

# devindocker
# {devindocker}

<!-- badges: start -->

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
```
70 changes: 60 additions & 10 deletions inst/renv/renv_instructions.Rmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: "Use renv"
title: "Use renv inside Docker container for package development"
---

```{r setup, include=FALSE}
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 121f72d

Please sign in to comment.