## ----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)) { cli::cli_inform("loading with {.pkg pkgload}") pkgload::load_all() } else { # fall back to the installed package (the path CRAN, CI, and pkgdown take) cli::cli_inform("loading with {.pkg library}") library(stamp) } old_opts <- st_opts_get() old_alias <- st_alias_get() ## ----------------------------------------------------------------------------- st_opts_reset() st_opts( versioning = "content", code_hash = TRUE, store_file_hash = TRUE, verify_on_load = TRUE, meta_format = "both" ) root <- tempfile("stamp-lineage-") st_init(root) # A pA <- fs::path(root, "A.qs") xA <- data.frame(a = 1:3) st_save(xA, pA, code = function(z) z, alias = NULL) # B depends on A pB <- fs::path(root, "B.qs") xB <- transform(xA, b = a * 2) st_save( xB, pB, code = function(z) z, parents = list(list(path = pA, version_id = st_latest(pA, alias = NULL))), alias = NULL ) # C depends on B pC <- fs::path(root, "C.qs") xC <- transform(xB, c = b + 1L) st_save( xC, pC, code = function(z) z, parents = list(list(path = pB, version_id = st_latest(pB, alias = NULL))), alias = NULL ) ## ----------------------------------------------------------------------------- # Immediate children of A (depth 1) st_children(pA, depth = 1, alias = NULL) # Full lineage (parents of an artifact) st_lineage(pC, depth = Inf, alias = NULL) ## ----------------------------------------------------------------------------- # Change A → new version xA2 <- transform(xA, a = a + 10L) st_save(xA2, pA, code = function(z) z, alias = NULL) # Strict staleness st_is_stale(pB) # TRUE (B's recorded A version is now old) st_is_stale(pC) # FALSE (C points to B, which hasn't changed yet) ## ----------------------------------------------------------------------------- # Strict: only B right now plan_strict <- st_plan_rebuild(pA, depth = Inf, mode = "strict") plan_strict # Propagate: includes B (level 1) and C (level 2) plan <- st_plan_rebuild(pA, depth = Inf, mode = "propagate") plan ## ----------------------------------------------------------------------------- # Register a builder for B: rebuild from A's committed version st_register_builder(pB, function(path, parents) { # parents is list(list(path=..., version_id=...)) A <- st_load_version(parents[[1]]$path, parents[[1]]$version_id, alias = NULL) list( x = transform(A, b = a * 2), code = function(z) z, code_label = "B <- A * 2" ) }) # Register a builder for C: rebuild from B's committed version st_register_builder(pC, function(path, parents) { B <- st_load_version(parents[[1]]$path, parents[[1]]$version_id, alias = NULL) list( x = transform(B, c = b + 1L), code = function(z) z, code_label = "C <- B + 1" ) }) # Dry run first (uses registered builders found by st_rebuild when rebuild_fun is NULL) st_rebuild(plan, dry_run = TRUE) # Now actually rebuild (will use registered builders) res <- st_rebuild(plan, dry_run = FALSE) res ## ----------------------------------------------------------------------------- st_is_stale(pB) st_is_stale(pC) st_plan_rebuild(pB, depth = Inf, mode = "propagate") ## ----------------------------------------------------------------------------- vroot <- fs::path_dir(st_info(pA, alias = NULL)$sidecar$path) vroot <- fs::path(vroot, "versions") if (fs::dir_exists(vroot)) { fs::dir_tree(vroot, recurse = TRUE, all = TRUE) } ## ----------------------------------------------------------------------------- st_info(pC, alias = NULL) ## ----cleanup, include=FALSE--------------------------------------------------- st_clear_builders(pB) st_clear_builders(pC) 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)