add R Markdown - knitr cache example & show default booleans

This commit is contained in:
Vincent Clemson 2022-06-19 23:22:26 -04:00
parent 14bc367517
commit e5e58a06aa
2 changed files with 83 additions and 39 deletions

View file

@ -31,8 +31,8 @@ If you are using this inside a container, a POSIX-compliant `tar` needs to be in
* `key` - An explicit key for restoring and saving the cache * `key` - An explicit key for restoring and saving the cache
* `restore-keys` - An ordered list of keys to use for restoring stale cache if no cache hit occurred for key. Note * `restore-keys` - An ordered list of keys to use for restoring stale cache if no cache hit occurred for key. Note
`cache-hit` returns false in this case. `cache-hit` returns false in this case.
* `reeval` - A boolean. Whether to reevaluate the `key` argument in the action's post run script when saving cache. Set to `true` if you would like your cache key set after your job's steps are complete. * `reeval` - A boolean. Default set to `false`. Whether to reevaluate the `key` argument in the action's post run script when saving cache. Set to `true` if you would like your cache key set after your job's steps are complete.
* `only-restore` - A boolean. Whether to only perform cache restoration and not the save post run script. * `only-restore` - A boolean. Default set to `false`. Whether to only perform cache restoration and not the save post run script.
### Outputs ### Outputs

View file

@ -1,5 +1,6 @@
# Examples # Examples
- [Examples](#examples)
- [C# - NuGet](#c---nuget) - [C# - NuGet](#c---nuget)
- [D - DUB](#d---dub) - [D - DUB](#d---dub)
- [POSIX](#posix) - [POSIX](#posix)
@ -9,7 +10,7 @@
- [macOS](#macos) - [macOS](#macos)
- [Windows](#windows-1) - [Windows](#windows-1)
- [Elixir - Mix](#elixir---mix) - [Elixir - Mix](#elixir---mix)
- [Erlang - Rebar3](#erlang--rebar3) - [Erlang - Rebar3](#erlang---rebar3)
- [Go - Modules](#go---modules) - [Go - Modules](#go---modules)
- [Linux](#linux-1) - [Linux](#linux-1)
- [macOS](#macos-1) - [macOS](#macos-1)
@ -31,6 +32,7 @@
- [Using pip to get cache location](#using-pip-to-get-cache-location) - [Using pip to get cache location](#using-pip-to-get-cache-location)
- [Python - pipenv](#python---pipenv) - [Python - pipenv](#python---pipenv)
- [R - renv](#r---renv) - [R - renv](#r---renv)
- [R Markdown - knitr cache](#r-markdown---knitr-cache)
- [Ruby - Bundler](#ruby---bundler) - [Ruby - Bundler](#ruby---bundler)
- [Rust - Cargo](#rust---cargo) - [Rust - Cargo](#rust---cargo)
- [Scala - SBT](#scala---sbt) - [Scala - SBT](#scala---sbt)
@ -515,6 +517,48 @@ For renv, the cache directory will vary by OS. The `RENV_PATHS_ROOT` environment
restore-keys: ${{ steps.get-version.outputs.os-version }}-${{ steps.get-version.outputs.r-version }}-${{inputs.cache-version }}- restore-keys: ${{ steps.get-version.outputs.os-version }}-${{ steps.get-version.outputs.r-version }}-${{inputs.cache-version }}-
``` ```
## R Markdown - knitr cache
For [R Markdown](https://bookdown.org/yihui/rmarkdown-cookbook/cache.html) and [Quarto](https://quarto.org/docs/computations/caching.html#knitr-cache), [knitr](https://yihui.org/knitr/options/#cache) creates a directory of cached code chunk variables & output for chunks where the chunk option `cache=TRUE` is set. The cache directory is typically located in the directory of the input file being rendered. It is named by concating the input `.Rmd` or `.qmd` file name with `_cache` appended.
See the composite action implementation [prncevince/r-actions/setup-knitr-cache](https://github.com/prncevince/r-actions/tree/main/setup-knitr-cache) and corresponding [example](https://github.com/prncevince/r-actions/blob/main/.github/workflows/knitr-cache.yaml) [workflows](https://github.com/prncevince/test-workflow-knitr-cache/blob/main/.github/workflows/knitr-cache.yaml).
Here, we set `reeval` to `true`. This instructs the action to reevalute the cache `key` input variable before saving the cache during the action's post script run when determing if we have met a cache hit. When using knitr caching, we do this because the knitr cache is typically ignored from the Git repo, and invalidation is determined by much more than a diff on the input file. Thus, it makes more sense to evaluate our cache key after knitr code chunk evaluation has completed. For more info on knitr cache invalidation, see [On Cache Invalidation](https://yihui.org/en/2018/06/cache-invalidation/).
```yaml
jobs:
setup-knitr-cache:
- uses: actions/checkout@v3
- uses: r-lib/actions/setup-pandoc@v2
- name: Install system dependencies on Linux
if: runner.os == 'Linux'
run: |
sudo apt-get update -y
sudo apt-get install -y libcurl4-openssl-dev libharfbuzz-dev libfribidi-dev
- uses: r-lib/actions/setup-r@v2
with:
r-version: latest
- name: Get R and OS version
id: restore-partials
shell: Rscript {0}
run: |
cat("##[set-output name=os-version;]", sessionInfo()$running, "\n", sep = "")
cat("##[set-output name=r-version;]", R.Version()$version.string, sep = "")
- name: Install R Markdown
shell: Rscript {0}
run: install.packages("rmarkdown")
- name: Restore knitr cache
uses: actions/cache@v3
with:
reeval: true
path: rmd-file-name_cache
key: ${{ github.action }}-${{ github.workflow }}-${{ steps.restore-partials.outputs.os-version }}-${{ steps.restore-partials.outputs.r-version }}-${{ hashFiles(format('{0}/**', rmd-file-name_cache)) }}
restore-keys: ${{ github.action }}-${{ github.workflow }}-${{ steps.restore-partials.outputs.os-version }}-${{ steps.restore-partials.outputs.r-version }}-
- name: Render R Markdown & Build knitr cache
shell: Rscript {0}
run: rmarkdown::render(input = "rmd-file-name.Rmd")
```
## Ruby - Bundler ## Ruby - Bundler
Caching gems with Bundler correctly is not trivial and just using `actions/cache` Caching gems with Bundler correctly is not trivial and just using `actions/cache`