run_script: Sources a script

View source: R/source.R

run_scriptR Documentation

Sources a script

Description

Basically base::source(), but prepending the library path with a touchstone library and running the script in a temp directory to avoid git operations like checking out different branches to interfere with the script execution (as running the script changes itself through git checkout).

Usage

run_script(
  path = "touchstone/script.R",
  branch = branch_get_or_fail("GITHUB_HEAD_REF")
)

Arguments

path

The script to run. It must fulfill the requirements of a touchstone_script.

branch

The branch that corresponds to the library that should be prepended to the library path when the script at path is executed, see 'Why this function?' below.

Value

The same as base::source(), which inherits from base::withVisible(), i.e. a list with value and visible (invisibly).

How to run this interactively?

You can use activate() to setup the environment to interactively run your script, as there are some adjustments needed to mirror the Github Action environment. In a GitHub Action workflow, the environment variables GITHUB_BASE_REF and GITHUB_HEAD_REF denote the target and source branch of the pull request - and these are default arguments in benchmark_run() (and other functions you probably want to call in your benchmarking script) to determinate the branches to use.

Why this function?

For isolation, {touchstone} does not allow the benchmarked package to be installed in the global package library, but only in touchstone libraries, as asserted with assert_no_global_installation(). However, this also implies that the package is not available in the touchstone script outside of benchmark runs (i.e. outside of benchmark_run(). We sometimes still want to call that package to prepare a benchmarking run though. To allow this, we prepend a touchstone library location that contains the installed benchmarked package for set-up tasks, and temporarily remove it during benchmarking with benchmark_run() so only one touchstone library is on the library path at any time.

Examples

## Not run: 
# assuming you want to compare the branch main with the branch devel
if (rlang::is_installed("withr")) {
  withr::with_envvar(
    c("GITHUB_BASE_REF" = "main", "GITHUB_HEAD_REF" = "devel"),
    run_script("touchstone/script.R")
  )
}

## End(Not run)

lorenzwalthert/touchstone documentation built on April 20, 2024, 9:57 a.m.