| Title: | Result Stability Checks for Empirical R Projects |
|---|---|
| Description: | Lightweight helpers for checking whether empirical results remain substantively unchanged across code revisions, platform differences, and package updates. The package supports regression-style testing of derived datasets, statistical model outputs, tables, and plots, helping researchers detect unintended result drift early and distinguish material from non-material changes in empirical workflows. |
| Authors: | Dianyi Yang [aut, cre, ctb] (ORCID: <https://orcid.org/0009-0004-4652-3429>) |
| Maintainer: | Dianyi Yang <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.2.1 |
| Built: | 2026-06-07 06:17:58 UTC |
| Source: | https://github.com/kv9898/resultcheck |
Removes a sandbox directory and all its contents. This should be called after testing is complete to free up disk space.
cleanup_sandbox(sandbox = NULL, force = TRUE)cleanup_sandbox(sandbox = NULL, force = TRUE)
sandbox |
Optional. A sandbox object created by |
force |
Logical. If TRUE (default), removes directory even if it contains files. |
Logical indicating success (invisible).
with_example({ sandbox <- setup_sandbox() cleanup_sandbox(sandbox) })with_example({ sandbox <- setup_sandbox() cleanup_sandbox(sandbox) })
Finds the root directory of the current R project using various heuristics.
The function searches for markers like _resultcheck.yml (preferred),
resultcheck.yml (legacy), .Rproj files,
or a .git directory. When running inside a sandbox created by
setup_sandbox(), it will search from the original working directory.
find_root(start_path = NULL)find_root(start_path = NULL)
start_path |
Optional. The directory to start searching from. If NULL (default), uses the current working directory or the stored original working directory if in a sandbox. |
The path to the project root directory.
with_example({ root <- find_root() print(root) })with_example({ root <- find_root() print(root) })
Executes an R script within a sandbox directory, suppressing messages, warnings, and graphical output. This is useful for testing empirical analysis scripts without polluting the console or creating unwanted plots.
run_in_sandbox( script_path, sandbox = NULL, suppress_messages = TRUE, suppress_warnings = TRUE, capture_output = TRUE )run_in_sandbox( script_path, sandbox = NULL, suppress_messages = TRUE, suppress_warnings = TRUE, capture_output = TRUE )
script_path |
Path to the R script to execute. |
sandbox |
Optional. A sandbox object created by |
suppress_messages |
Logical. Whether to suppress messages (default: TRUE). |
suppress_warnings |
Logical. Whether to suppress warnings (default: TRUE). |
capture_output |
Logical. Whether to capture output (default: TRUE). |
Invisible TRUE on successful execution.
with_example({ sandbox <- setup_sandbox() on.exit(cleanup_sandbox(sandbox), add = TRUE) run_in_sandbox("analysis.R", sandbox) })with_example({ sandbox <- setup_sandbox() on.exit(cleanup_sandbox(sandbox), add = TRUE) run_in_sandbox("analysis.R", sandbox) })
Creates a temporary directory and copies specified files and/or directories into it while preserving their path structure. This is useful for testing empirical analysis scripts in isolation.
setup_sandbox(files = NULL, temp_base = NULL)setup_sandbox(files = NULL, temp_base = NULL)
files |
Character vector of relative file or directory paths to copy
to the sandbox. Leave as |
temp_base |
Optional. Custom location for the temporary directory.
If NULL (default), uses |
A list with class "resultcheck_sandbox" containing:
path |
The path to the created temporary directory |
id |
A unique timestamp-based identifier for this sandbox |
with_example({ sandbox <- setup_sandbox() print(sandbox$path) cleanup_sandbox(sandbox) })with_example({ sandbox <- setup_sandbox() print(sandbox$path) cleanup_sandbox(sandbox) })
Creates or updates a snapshot of an R object for interactive analysis. On first use, saves the object to a human-readable snapshot file (.md). On subsequent uses, compares the current object to the saved snapshot.
snapshot(value, name, script_name = NULL, method = NULL)snapshot(value, name, script_name = NULL, method = NULL)
value |
The R object to snapshot (e.g., plot, table, model output). |
name |
Character. A descriptive name for this snapshot. |
script_name |
Optional. The name of the script creating the snapshot.
If NULL, auto-detects via |
method |
Optional function or non-empty list of functions used to
serialize |
In interactive mode (default), prompts the user to update if differences are found and emits a warning. In testing mode (inside testthat or run_in_sandbox), throws an error if snapshot doesn't exist or doesn't match.
Snapshots are stored under tests/_resultcheck_snaps/ by default,
organized by script name, and configurable via snapshot.dir in
_resultcheck.yml. Method defaults can be configured via
snapshot.method and class-specific defaults via
snapshot.method_by_class. Optional class defaults can also be loaded
from an R file using snapshot.method_defaults_file. Method strings in
config (for example "print + str" or "stats::coef") are
resolved to callable functions. In config expressions, "+" is treated
as the method delimiter.
Built-in class defaults (loaded from inst/extdata/snapshot-method-defaults.R) use broom functions for many statistical model classes. The default method is typically broom::tidy, with broom::glance and/or broom::augment added where supported (per the broom available-methods table at https://broom.tidymodels.org/articles/available-methods.html).
Invisible TRUE if snapshot matches or was updated. In testing mode, throws an error if snapshot is missing or doesn't match.
with_example({ model <- stats::lm(mpg ~ wt, data = datasets::mtcars) snapshot(model, "model_default", script_name = "analysis") snapshot(model, "model_multi", script_name = "analysis", method = list(summary = summary, print = print)) snapshot(model, "model_print", script_name = "analysis", method = print) snapshot(model, "model_ns", script_name = "analysis", method = stats::coef) snapshot(model, "model_length", script_name = "analysis", method = length) }) with_example({ sandbox <- setup_sandbox() on.exit(cleanup_sandbox(sandbox), add = TRUE) run_in_sandbox("analysis.R", sandbox) }) if (interactive()) with_example({ sandbox <- setup_sandbox() on.exit(cleanup_sandbox(sandbox), add = TRUE) run_in_sandbox("analysis.R", sandbox) }, mismatch = TRUE)with_example({ model <- stats::lm(mpg ~ wt, data = datasets::mtcars) snapshot(model, "model_default", script_name = "analysis") snapshot(model, "model_multi", script_name = "analysis", method = list(summary = summary, print = print)) snapshot(model, "model_print", script_name = "analysis", method = print) snapshot(model, "model_ns", script_name = "analysis", method = stats::coef) snapshot(model, "model_length", script_name = "analysis", method = length) }) with_example({ sandbox <- setup_sandbox() on.exit(cleanup_sandbox(sandbox), add = TRUE) run_in_sandbox("analysis.R", sandbox) }) if (interactive()) with_example({ sandbox <- setup_sandbox() on.exit(cleanup_sandbox(sandbox), add = TRUE) run_in_sandbox("analysis.R", sandbox) }, mismatch = TRUE)
Creates a self-contained example project under tempdir(), including:
_resultcheck.yml (project root marker)
analysis.R with snapshot(model, "model")
matching and mismatched snapshot files
tests/testthat/test-analysis.R
then temporarily sets the working directory to that project while
evaluating code.
with_example(code, mismatch = FALSE)with_example(code, mismatch = FALSE)
code |
Code to evaluate inside the temporary example project. |
mismatch |
Logical. If TRUE, replaces the active snapshot with a
mismatched version before evaluating |
The value of code.
with_example({ root <- find_root() print(root) })with_example({ root <- find_root() print(root) })