Skip to content

r-lib/devcontainer-templates

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dev container templates for R projects

Common features

  • Ubuntu 22.04 base image.
  • R 4.4.x via rig.
  • Package dependencies are specified in a DESCRIPTION file, as Dependes: or Imports:.
  • Package dependencies for tests are specified in DESCRIPTION, under Suggests:.
  • The test stage of Dockerfile runs the tests.
  • The prod stage of Dockerfile runs the tests cases via testthat::test_lcal(), but does not include them in the image.

Example project

See the https://github.com/r-hub/cran-metadata project that uses these dev container templates.

Templates

  • gh-script An R script to run with a scheduler on GitHub Actions.

VS Code installation

  1. From the command palette, select "Dev Containers: Add Dev Container Configuration Files...".
  2. Select "Add configuration to workspace" or "Add configuration to user data folder".
  3. Then use ghcr.io/r-lib/devcontainer-templates/gh-script:latest as the template to apply.
  4. Select additional features. None are required currently.
  5. When including optional dependencies you may want to include some GitHub actions workflows to deploy the script to GitHub Actions.
  6. Create a DESCRIPTION file with package name and version. E.g.
    Package: mypackage
    Version: 1.0.0
    
  7. You now select "Rebuild and Reopen in Container" to start the project in in the dev container.

Command line installation

  1. Install the devcontainer cli. On macOS you can use brew install devcontainer
  2. Before installing the template, I recommend to commit your changes, because the template might overwrite some of your files.
  3. Install the template:
    devcontainer templates apply -w . -t \
      ghcr.io/r-lib/devcontainer-templates/gh-script:latest
    
  4. Create a DESCRIPTION file with package name and version. E.g.
    Package: mypackage
    Version: 1.0.0
    
  5. Build the template:
    devcontainer build --workspace-folder .
    
  6. Start the container:
    devcontainer up --workspace-folder .
    

Usage

Use the template as usual in tools that natively support dev containers, e.g. VS Code, GitHub Codespaces, VSCodium + Devpod, etc.

Positron

To use R projects with these templates from Poisitron, for now there is a bit of extra legwork:

  1. Use a random port for SSH (optional).
    echo PORT=2222 >> .env
    
  2. Start the container:
    devcontainer up --workspace-folder .
    
  3. Connect from Positron. From the command palette choose 'Remote-SSH: Connect to Host...' or 'Remote-SSH: Connect Current Window to Host...'. The connection string is
    root@localhost:2222
    
    (Use the port you set in .env. The default is 2222 if you don't have .env.)
  4. Open the /workspaces/cran-metadata folder.
  • Reconnecting from Positron: to reconnect to a running dev container, the easiest is to select the remote folder from the list of recently opened project on the top right. If it is not there, use the last two steps of the previous section.
  • Stopping: Positron does not stop the container when it disconnects. The devcontainer cli also cannot stop it currently. To stop the container call docker directly:
    docker stop cran-metadata-r-app-1
    
  • Restarting: to restart a stopped container call
    devcontainer up --workspace-folder .
    
  • Rebuilding: to rebuild a container stop it and delete it before you build it again:
    docker stop cran-metadata-r-app-1
    docker rm cran-metadata-r-app-1
    devcontainer up --workspace-folder .
    

License

MIT (c) Posit, PBC