## ----setup-------------------------------------------------------------------- # Use development build when interactive *and* explicitly enabled via env var. dev_mode <- (Sys.getenv("DEV_VIGNETTES", "false") == "true") if (dev_mode && requireNamespace("pkgload", quietly = TRUE)) { pkgload::load_all( export_all = FALSE, helpers = FALSE, attach_testthat = FALSE ) } else { # fall back to the installed package (the path CRAN, CI, and pkgdown take) library(stamp) } old_opts <- st_opts_get() old_alias <- st_alias_get() ## ----------------------------------------------------------------------------- st_opts_reset() st_opts( versioning = "content", # skip write when content unchanged code_hash = TRUE, # store code hash when 'code=' is provided to st_save() store_file_hash = TRUE, # compute & store file hash after write verify_on_load = TRUE, # verify content on load (warn on mismatch) meta_format = "both" # write JSON + QS2 sidecars ) ## ----------------------------------------------------------------------------- root <- tempfile("stamp-hashing-") st_init(root) p <- "demo.qs" x <- data.frame(a = 1:3) # First write: creates artifact + sidecars + catalog entry st_save(x, p, code = function(z) z, alias = NULL) # Second write, same content & same code: skipped (no new version) st_save(x, p, code = function(z) z, alias = NULL) nrow(st_versions(p, alias = NULL)) # should be 1 ## ----------------------------------------------------------------------------- x2 <- transform(x, a = a + 1L) st_save(x2, p, code = function(z) z, alias = NULL) nrow(st_versions(p, alias = NULL)) # now 2 st_latest(p, alias = NULL) # latest version id (string) ## ----------------------------------------------------------------------------- meta <- st_read_sidecar(p, alias = NULL) meta[c( "format", "created_at", "size_bytes", "content_hash", "code_hash", "file_hash" )] ## ----------------------------------------------------------------------------- x_same <- x2 x_new <- transform(x2, a = a + 10L) st_changed(p, x = x_same, code = function(z) z, alias = NULL) st_changed_reason(p, x = x_same, code = function(z) z, alias = NULL) # "no_change" st_changed(p, x = x_new, code = function(z) z, alias = NULL) st_changed_reason(p, x = x_new, code = function(z) z, alias = NULL) # "content" st_should_save(p, x = x_same, code = function(z) z, alias = NULL) # recommends skip st_should_save(p, x = x_new, code = function(z) z, alias = NULL) # recommends save ## ----------------------------------------------------------------------------- vids <- st_versions(p, alias = NULL) head(vids) vid_latest <- st_latest(p, alias = NULL) obj_latest <- st_load_version(p, vid_latest, alias = NULL) # Load an older version by id if (nrow(vids) > 1L) { vid_old <- vids$version_id[[nrow(vids)]] obj_old <- st_load_version(p, vid_old, alias = NULL) } ## ----------------------------------------------------------------------------- p <- "demo.qs" x <- data.frame(a = 1:5) # Write once to create a version snapshot st_save(x, p, code = function(z) z, alias = NULL) # Version history is stored next to the artifact info <- st_info(p, alias = NULL) artifact_dir <- fs::path_dir(info$sidecar$path) # e.g., root/demo.qs/ vroot <- fs::path(artifact_dir, "versions") if (fs::dir_exists(vroot)) { cat("Versions directory:", vroot, "\n\n") fs::dir_tree(vroot, recurse = TRUE, all = TRUE) } else { cat("No versions directory found (versioning may be disabled)\n") } ## ----------------------------------------------------------------------------- invisible(st_load(p, alias = NULL)) # triggers optional verify; warns on mismatch ## ----cleanup, include=FALSE--------------------------------------------------- suppressMessages(do.call(st_opts, old_opts)) if (!is.null(old_alias)) { st_init(old_alias$root, state_dir = old_alias$state_dir, verbose = FALSE) } unlink(root, recursive = TRUE)