snapr 
The goal of snapr is to provide convenient snapshot testing functions for R packages:
expect_snapshot_data()- for data.frames (adapted with permission from the{ssdtools}package)expect_snapshot_object()- generalizesexpect_snapshot_data()for use with any R object
Installation
You can install the released version of snapr from CRAN with:
install.packages("snapr")You can install the development version of snapr from GitHub with:
# install.packages("pak")
pak::pak("d-morrison/snapr")Optional: enhanced RDS diffs with diffviewer
snapr works best with the d-morrison/diffviewer fork of diffviewer, which wraps diffobj to provide rich, visual diffs when reviewing RDS snapshots via testthat::snapshot_review(). Install it with:
pak::pak("d-morrison/diffviewer")Examples
Snapshot data.frames
library(snapr)
# In a testthat test file:
test_that("iris data is correct", {
expect_snapshot_data(iris[1:5, ], name = "iris_sample")
})Snapshot any R object
library(snapr)
# Snapshot a list (RDS format by default - works for any R object)
test_that("config object is correct", {
config <- list(
name = "my_app",
version = "1.0.0",
settings = list(debug = TRUE, timeout = 30)
)
expect_snapshot_object(config, name = "config")
})
# Snapshot with JSON format for human-readable diffs
test_that("simple data is correct", {
data <- list(x = 1:5, y = letters[1:5])
expect_snapshot_object(data, name = "simple_data", writer = save_json)
})
# Snapshot a model (use RDS for complex objects)
test_that("model structure is correct", {
model <- lm(mpg ~ wt, data = mtcars)
expect_snapshot_object(model, name = "model")
})
