Skip to content

Commit

Permalink
Add prune param to setup-r-dependencies to prune extra R packages…
Browse files Browse the repository at this point in the history
… only when `cache="always"`
  • Loading branch information
schloerke committed Feb 14, 2024
1 parent 6297893 commit 47ee010
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
4 changes: 3 additions & 1 deletion setup-r-dependencies/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ This action install dependencies for the current R environment based on the DESC
Inputs available

- `cache` - default `true`. Whether packages should be cached across runs or
not.
not. If `"always"` is provided, the package cache will be saved even if the Action fails.
- `cache-version` - default `1`. If you need to invalidate the existing
cache pass any other number and a new cache will be used. Ignored if
`cache: false`. Note that you can also [delete caches
Expand All @@ -40,6 +40,8 @@ Inputs available
you need quoting. Defaults to `FALSE`.
- `working-directory` - default `'.'`. If the DESCRIPTION file is not in the
root directory of your repository.
- `prune` - default `true`. Used when `cache="always"`. Whether to prune the R
library of packages not found within the lock file used for installation.

Basic:
```yaml
Expand Down
46 changes: 43 additions & 3 deletions setup-r-dependencies/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: 'Action to setup installation tools and install R dependencies'
author: 'Jim Hester'
inputs:
cache:
description: 'A boolean value indicating whether packages should be cached (on success) from one to the other. If `"always"` is provided, the package cache will be saved even if the Action fails.'
description: 'Whether packages should be cached across runs or not. If `"always"` is provided, the package cache will be saved even if the Action fails.'
required: true
default: true
cache-version:
Expand All @@ -29,6 +29,9 @@ inputs:
upgrade:
description: 'Whether to install the latest available versions of the dependencies. Must be an R expression. See the README for details if you need quoting.'
default: 'FALSE'
prune:
description: 'Whether to prune the R library of packages not found within the lock file used for installation.'
default: true
runs:
using: "composite"
steps:
Expand Down Expand Up @@ -145,13 +148,50 @@ runs:
restore-keys: ${{ steps.cache-args.outputs.restore-keys }}

- name: Install dependencies
id: install-pkgs
run: |
# Install/Update packages
cat("::group::Install/update packages\n")
Sys.setenv("PKGCACHE_HTTP_VERSION" = "2")
library(pak, lib.loc = Sys.getenv("R_LIB_FOR_PAK"))
pak::lockfile_install(".github/pkg.lock")
## Clean up lock file
pkgs <- pak::lockfile_install(".github/pkg.lock")
# Store the installed packages
packages <- paste0(pkgs$package, collapse = "|")
cat("packages=", packages, "\n", file = Sys.getenv("GITHUB_OUTPUT"), sep = "", append = TRUE)
cat("::endgroup::\n")
shell: Rscript {0}
working-directory: ${{ inputs.working-directory }}

- name: Prune unused R packages
if: inputs.cache == 'always' && inputs.prune == 'true'
run: |
# Prune packages
cat("::group::Prune unnecessary packages\n")
installed_df <- as.data.frame(installed.packages(), stringsAsFactors = FALSE)
installed_packages <- installed_df$Package
core_pkgs <- c(
"translations", # Core package on windows
installed_packages[!is.na(installed_df$Priority)]
)
pak_pkgs <- c(
"pak", # Never remove pak
strsplit("${{ steps.install-pkgs.outputs.packages }}", "|", fixed = TRUE)[[1]]
)
to_remove_pkgs <- setdiff(installed_packages, c(core_pkgs, pak_pkgs))
if (length(to_remove_pkgs) > 0) {
cat("Removing unnecessary packages: ", paste0(to_remove_pkgs, collapse = ", "), "\n")
remove.packages(to_remove_pkgs)
} else {
cat("No unnecessary packages to remove\n")
}
cat("::endgroup::\n")
shell: Rscript {0}
working-directory: ${{ inputs.working-directory }}

- name: Clean up lock file
run: |
# Clean up lock file
cat("::group::Clean up lock file\n")
unlink(".github/pkg.lock")
cat("::endgroup::\n")
shell: Rscript {0}
Expand Down

0 comments on commit 47ee010

Please sign in to comment.