knitr::opts_chunk$set(
  eval = FALSE
)

First time

Follow each step carefully until section "You can restart..."

Work inside a project

Start you project : Click on the ".Rproj" file in your project directory.

Set your RStudio to show hidden files

Files pane > More > Show Hidden Files

You may need {renv}

remotes::install_github('rstudio/renv')

Hide from git

# Should already be done
# install.packages("usethis")
usethis::use_git_ignore("renv_instructions.Rmd")
usethis::use_build_ignore("renv_instructions.Rmd")

The first time you launch your project in the Docker container, run :

# Project library position: 
# This is default, uncomment is not necessary.
# Sys.setenv(RENV_PATHS_LIBRARY_ROOT = "renv/library") 
# However, if you set library out of the project, 
# change in accordance with `renv_out = TRUE` then use:
# Sys.setenv(RENV_PATHS_LIBRARY_ROOT = "~/.renv/library")

renv::consent(TRUE) # Open the Console to answer question to proceed
renv::activate()

Set up shared cache and initiate with {renv installation}

# Set up cache
Sys.setenv(RENV_PATHS_CACHE = "/opt/local/renv/cache")
renv::settings$use.cache(TRUE)
# Install {renv} in global cache
renv::install("renv")

Setup .Rprofile content

Store the following code inside your project .Rprofile file to reduce problems with cache (Open .Rprofile with: usethis::edit_r_profile(scope = "project")). Add code in this order:

# If you have a user .Rprofile inside the container, you may want to uncomment this
# source("~/.Rprofile")

# Project library position: 
# This is default, uncomment is not necessary.
# Sys.setenv(RENV_PATHS_LIBRARY_ROOT = "renv/library") 
# However, if you set library out of the project, 
# change in accordance with `renv_out = TRUE` then use:
# Sys.setenv(RENV_PATHS_LIBRARY_ROOT = "~/.renv/library")

source("renv/activate.R")
renv::activate()

# cache ----
if (dir.exists("/opt/local/renv/cache")) {
  # Cache inside the docker container with persistent drive
  # shared on host
  Sys.setenv(RENV_PATHS_CACHE = "/opt/local/renv/cache")
  renv::settings$use.cache(TRUE)
} else {
  # No cache
  renv::settings$use.cache(FALSE)
}

Restart your RStudio session

You can now stop Rstudio server and stop the container. Then re-launch container to verify everything is correctly set up

Do not worry now about different {renv} versions
End of initialization. Then you can have a look at following commands for dev.

If you have {renv} mismatch messages

Follow instructions of {renv} in your Console. This might be running:

renv::snapshot()

If you have broken symlinks at restart

This is due to the change of the global cache. You will need to re-install packages as follows:

ll <- list.files(.libPaths()[1], full.names = FALSE)
for (l in ll) {
  try(install.packages(l))
}

Restart your Rstudio session. There should not be anymore symlinks problems.
Try again to stop the RStudio session, stop the container and relaunch to verify installation.

Restore the project from the lock file if any problem

renv::init()

During the dev process

Install packages

Prefer using renv::install() for your package installations to be sure it uses the global {renv} cache et symbolic links.

# From doc: ?renv::install
# install the latest version of 'digest' on CRAN
renv::install("digest")

# install an old version of 'digest' (using CRAN archives)
renv::install("digest@0.6.18")

# install 'digest' from GitHub (latest dev. version)
renv::install("eddelbuettel/digest")

# install a package from GitHub, using specific commit
renv::install("eddelbuettel/digest@df55b00bff33e945246eff2586717452e635032f")

# install a package from Bioconductor
# (note: requires the BiocManager package)
renv::install("bioc::Biobase")

# install from gitlab.com
renv::install("gitlab::jimhester/covr")

# install from random git
renv::install("git::git@bitbucket.org:djnavarro/lsr.git")

# install a package from local sources
renv::install("~/path/to/package")

After you updated packages, added new ones or removed somes

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:

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 :

# Update DESCRIPTION file
attachment::att_amend_desc()
# List dependencies
custom_packages <- c(attachment::att_from_description(),
                     "renv", "knitr",
                     "devtools", "roxygen2", "usethis",
                     "testthat", "covr", "attachment",
                     "pkgdown")
# Snapshot
renv::snapshot(packages = custom_packages)

If you are not satisfied, run:

renv::restore() # instead of snapshot()

If you are ready to send your modifications to the git server

And after you ran devtools::check(), store your packages list with:

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", "knitr",
                     "devtools", "roxygen2", "usethis",
                     "testthat", "covr", "attachment",
                     "pkgdown")
renv::snapshot(packages = custom_packages)

If you updated your branch from the server

There may be new packages needed, therefore run:

renv::restore()

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:

renv::upgrade()

Upgrade or fix CRAN repository

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:

remotes::install_version("golem", version = "0.3.0", repos = "https://cloud.r-project.org")

Use git inside the Docker container

First time

# project config
usethis::use_git_config(scope = "project", user.name = "username", user.email = "user@email.fr")

Use credentials

Must be done for each session as not stored in the persistent drive

# Store credentials
my_cred <- git2r::cred_user_pass(
  username = "username",
  password = askpass::askpass()
)
usethis::use_git_credentials(credentials = my_cred)

If you mistakenly installed package from github or forced one version

You may have to be sure to install dependencies from MRAN

ll <- list.files(.libPaths()[1], full.names = TRUE)
for (l in ll) {
  try(remotes::install_cran(l))
}


ThinkR-open/devindocker documentation built on Jan. 28, 2021, 12:42 p.m.