Stamp

The Problem

You have multiple input files produced by different processes. You want to read, process, and save derived artifacts while capturing.

Let’s simulate the data and folder structure:

# 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)
}
library(data.table)
#> 
#> Attaching package: 'data.table'
#> The following object is masked from 'package:base':
#> 
#>     %notin%
old_opts <- st_opts_get()
old_alias <- st_alias_get()
set.seed(123)

# Create an isolated temporary root so all writes occur outside the package tree.
# Use a short name to avoid long path issues on Windows (PATH_MAX = 260).
root_dir <- tempfile("s") |>
  fs::dir_create()

# Initialize stamp under this temp root
st_init(root = root_dir)
#> ℹ Rebasing default alias to new folder.
#>   Existing: /tmp/RtmpCXgvOI/stamp-hashing-b56711b2d8/.stamp
#>   Requested: /tmp/RtmpCXgvOI/sb561479a059/.stamp
#> ✔ stamp initialized
#>   alias: default
#>   root: /tmp/RtmpCXgvOI/sb561479a059
#>   state: /tmp/RtmpCXgvOI/sb561479a059/.stamp

# Helper: list only data files with known extensions to avoid locks/sidecars
# Returns relative paths from alias root (suitable for st_load)
data_files <- function(dir) {
  # Convert dir to relative path from root_dir if it's absolute
  rel_dir <- if (fs::is_absolute_path(dir)) {
    as.character(fs::path_rel(dir, start = root_dir))
  } else {
    dir
  }
  
  # Get absolute path for listing
  abs_dir <- fs::path(root_dir, rel_dir)
  if (!fs::dir_exists(abs_dir)) {
    return(character(0))
  }
  
  f <- fs::dir_ls(abs_dir)
  ext <- tolower(fs::path_ext(f))
  allowed <- c("qs2", "rds", "csv", "fst", "json")
  f <- f[ext %in% allowed]
  
  # Return relative paths from root_dir
  as.character(fs::path_rel(f, start = root_dir))
}

The Solution

Harmonizing Economic Indicators with Versioning & Lineage

We walk through a realistic data engineering scenario: several country/year micro datasets and macro indicators must be combined into a standardized output table. We want:

  1. Consistent saving, versioning, and timestamp metadata for each input artifact.
  2. Explicit primary keys (PK) recorded on disk.
  3. A derived summary table saved with full lineage (parents listed).
  4. Demonstrating how a single input change propagates using st_is_stale(), st_plan_rebuild(), and st_rebuild().
  5. Adding new data (ARG 2015) and observing minimal recomputation.
  • Countries/years for welfare (micro) data: COL2010, COL2012, MEX2010, MEX2015, PRY2011, PRY2014.

  • Reporting levels:

    • COL and MEX: both urban and rural.
    • PRY: only national.
  • Macro data (CPI, GDP, population) exist for COL, MEX, PRY, BRA, ARG (2010–2015). CPI & population have all three reporting levels (national, urban, rural) for every country-year.

1. Simulate and Save Input Micro Data

We’ll create small synthetic micro datasets with columns: country, year, reporting_level, hh_id, welfare, weight.

welfare_specs <- data.frame(
  country = c("COL", "COL", "MEX", "MEX", "PRY", "PRY"),
  year = c(2010, 2012, 2010, 2015, 2011, 2014),
  stringsAsFactors = FALSE
)

mk_reporting <- function(cty) {
  if (cty %in% c("COL", "MEX")) c("urban", "rural") else c("national")
}

simulate_welfare <- function(country, year) {
  rl <- mk_reporting(country)
  # 200 households per reporting level
  dt_list <- lapply(rl, function(rlev) {
    n <- 200L
    data.table(
      country = country,
      year = year,
      reporting_level = rlev,
      hh_id = sprintf("%s_%s_%s_%03d", country, year, rlev, seq_len(n)),
      welfare = round(
        rlnorm(n, meanlog = log(1000 + year) - 7, sdlog = 0.5),
        2
      ),
      weight = runif(n, 0.5, 2)
    )
  })
  rbindlist(dt_list)
}

welfare_paths <- list()
for (i in seq_len(nrow(welfare_specs))) {
  row <- welfare_specs[i, ]
  dt <- simulate_welfare(row$country, row$year)
  fn <- sprintf("data/welfare/%s_%d.qs2", row$country, row$year)
  # Primary key columns include household id for uniqueness
  st_save(
    dt,
    fn,
    pk = c("country", "year", "reporting_level", "hh_id"),
    domain = "welfare",
    alias = NULL
  )
  welfare_paths[[length(welfare_paths) + 1]] <- fn
}
#> ✔ Saved [qs2] → '/tmp/RtmpCXgvOI/sb561479a059/data/welfare/COL_2010.qs2' @
#>   version db502d2ae8e7b80e
#> ✔ Saved [qs2] → '/tmp/RtmpCXgvOI/sb561479a059/data/welfare/COL_2012.qs2' @
#>   version b1dc3d7a82e87efd
#> ✔ Saved [qs2] → '/tmp/RtmpCXgvOI/sb561479a059/data/welfare/MEX_2010.qs2' @
#>   version 9b11661e02862916
#> ✔ Saved [qs2] → '/tmp/RtmpCXgvOI/sb561479a059/data/welfare/MEX_2015.qs2' @
#>   version 5aaeb0e8b8771f6f
#> ✔ Saved [qs2] → '/tmp/RtmpCXgvOI/sb561479a059/data/welfare/PRY_2011.qs2' @
#>   version b82a0f3ec9595e37
#> ✔ Saved [qs2] → '/tmp/RtmpCXgvOI/sb561479a059/data/welfare/PRY_2014.qs2' @
#>   version 1471e74bf4044e4b
unlist(welfare_paths)
#> [1] "data/welfare/COL_2010.qs2" "data/welfare/COL_2012.qs2"
#> [3] "data/welfare/MEX_2010.qs2" "data/welfare/MEX_2015.qs2"
#> [5] "data/welfare/PRY_2011.qs2" "data/welfare/PRY_2014.qs2"

Each call to st_save() assigns a version id (if content differs) and writes sidecar metadata including PK.

Inspect one artifact:

st_info("data/welfare/COL_2010.qs2", alias = NULL)
#> $sidecar
#> $sidecar$path
#> [1] "/tmp/RtmpCXgvOI/sb561479a059/data/welfare/COL_2010.qs2"
#> 
#> $sidecar$format
#> [1] "qs2"
#> 
#> $sidecar$created_at
#> [1] "2026-12-04T20:09:35.634125Z"
#> 
#> $sidecar$size_bytes
#> [1] 4795
#> 
#> $sidecar$content_hash
#> [1] "5780152e3bedc476"
#> 
#> $sidecar$code_hash
#> NULL
#> 
#> $sidecar$file_hash
#> NULL
#> 
#> $sidecar$code_label
#> NULL
#> 
#> $sidecar$parents
#> list()
#> 
#> $sidecar$attrs
#> $sidecar$attrs$stamp_data_table
#> $sidecar$attrs$stamp_data_table$version
#> [1] 1
#> 
#> $sidecar$attrs$stamp_data_table$key
#> list()
#> 
#> 
#> 
#> $sidecar$pk
#> $sidecar$pk$keys
#> [1] "country"         "year"            "reporting_level" "hh_id"          
#> 
#> 
#> $sidecar$domain
#> [1] "welfare"
#> 
#> 
#> $catalog
#> $catalog$latest_version_id
#> [1] "db502d2ae8e7b80e"
#> 
#> $catalog$n_versions
#> [1] 1
#> 
#> 
#> $snapshot_dir
#> /tmp/RtmpCXgvOI/sb561479a059/data/welfare/COL_2010.qs2/versions/db502d2ae8e7b80e
#> 
#> $parents
#> list()

2. Simulate and Save Macro Data (CPI, GDP, Population)

CPI & population are reporting-level granular; GDP is country-year only.

years <- 2010:2015
countries_macro <- c("COL", "MEX", "PRY", "BRA", "ARG")
levels_all <- c("national", "urban", "rural")

simulate_cpi <- function() {
  out <- CJ(
    country = countries_macro,
    year = years,
    reporting_level = levels_all
  )
  out[, cpi := round(runif(.N, 80, 140), 2)]
  out
}

simulate_population <- function() {
  out <- CJ(
    country = countries_macro,
    year = years,
    reporting_level = levels_all
  )
  out[, population := round(runif(.N, 5e5, 20e6))]
  out
}

simulate_gdp <- function() {
  out <- CJ(country = countries_macro, year = years)
  out[, gdp := round(runif(.N, 1e9, 5e11))]
  out
}

cpi <- simulate_cpi()
pop <- simulate_population()
gdp <- simulate_gdp()

st_save(
  cpi,
  "data/macro/cpi.qs2",
  pk = c("country", "year", "reporting_level"),
  domain = "macro",
  alias = NULL
)
#> ✔ Saved [qs2] → '/tmp/RtmpCXgvOI/sb561479a059/data/macro/cpi.qs2' @ version
#>   5de2873364de9333
st_save(
  pop,
  "data/macro/population.qs2",
  pk = c("country", "year", "reporting_level"),
  domain = "macro",
  alias = NULL
)
#> ✔ Saved [qs2] → '/tmp/RtmpCXgvOI/sb561479a059/data/macro/population.qs2' @
#>   version 38351a189b2e146a
st_save(
  gdp,
  "data/macro/gdp.qs2",
  pk = c("country", "year"),
  domain = "macro",
  alias = NULL
)
#> ✔ Saved [qs2] → '/tmp/RtmpCXgvOI/sb561479a059/data/macro/gdp.qs2' @ version
#>   9d1d2f36fd6b1622

Version listing for CPI:

print(st_versions("data/macro/cpi.qs2", alias = NULL))
#>          version_id      artifact_id     content_hash code_hash size_bytes
#>              <char>           <char>           <char>    <char>      <num>
#> 1: 5de2873364de9333 003b5905aba993e3 b678a67494dd3300      <NA>        886
#>                     created_at sidecar_format
#>                         <char>         <char>
#> 1: 2026-12-04T20:09:35.872195Z           json

3. Aggregation Function foo() and Output Table

We compute per (country, year, reporting_level) welfare statistics and merge with CPI, GDP, population. We demonstrate loading inputs inside the function for self-containment.

foo <- function() {
  # Load all welfare micro artifacts (only recognized data files)
  welfare_files <- data_files("data/welfare")
  if (length(welfare_files) == 0) {
    cat("No welfare files found; returning empty table.\n")
    return(data.table::data.table())
  }
  wf <- rbindlist(lapply(welfare_files, st_load))

  stats <- wf[,
    .(
      welfare_mean = weighted.mean(welfare, weight),
      welfare_median = as.numeric(stats::median(welfare)),
      welfare_sd = stats::sd(welfare),
      welfare_n = .N
    ),
    by = .(country, year, reporting_level)
  ]

  cpi <- st_load("data/macro/cpi.qs2", alias = NULL)
  gdp <- st_load("data/macro/gdp.qs2", alias = NULL)
  pop <- st_load("data/macro/population.qs2", alias = NULL)

  # Merge; CPI & population contain rows not present in welfare; keep welfare rows only
  out <- merge(
    stats,
    cpi,
    by = c("country", "year", "reporting_level"),
    all.x = TRUE
  )
  out <- merge(
    out,
    pop,
    by = c("country", "year", "reporting_level"),
    all.x = TRUE
  )
  out <- merge(out, gdp, by = c("country", "year"), all.x = TRUE)
  setcolorder(
    out,
    c(
      "country",
      "year",
      "reporting_level",
      "welfare_mean",
      "welfare_median",
      "welfare_sd",
      "welfare_n",
      "cpi",
      "gdp",
      "population"
    )
  )
  out
}

# Call foo() to compute summary (empty if no data found)
summary_table <- foo()
#> ✔ Loaded [qs2] ← '/tmp/RtmpCXgvOI/sb561479a059/data/welfare/COL_2010.qs2'
#> ✔ Loaded [qs2] ← '/tmp/RtmpCXgvOI/sb561479a059/data/welfare/COL_2012.qs2'
#> ✔ Loaded [qs2] ← '/tmp/RtmpCXgvOI/sb561479a059/data/welfare/MEX_2010.qs2'
#> ✔ Loaded [qs2] ← '/tmp/RtmpCXgvOI/sb561479a059/data/welfare/MEX_2015.qs2'
#> ✔ Loaded [qs2] ← '/tmp/RtmpCXgvOI/sb561479a059/data/welfare/PRY_2011.qs2'
#> ✔ Loaded [qs2] ← '/tmp/RtmpCXgvOI/sb561479a059/data/welfare/PRY_2014.qs2'
#> ✔ Loaded [qs2] ← '/tmp/RtmpCXgvOI/sb561479a059/data/macro/cpi.qs2'
#> ✔ Loaded [qs2] ← '/tmp/RtmpCXgvOI/sb561479a059/data/macro/gdp.qs2'
#> ✔ Loaded [qs2] ← '/tmp/RtmpCXgvOI/sb561479a059/data/macro/population.qs2'
if (nrow(summary_table) > 0) {
  head(summary_table)
} else {
  cat("Summary table is empty; ensure welfare data was saved.\n")
  summary_table
}
#> Key: <country, year>
#>    country  year reporting_level welfare_mean welfare_median welfare_sd
#>     <char> <int>          <char>        <num>          <num>      <num>
#> 1:     COL  2010           rural     3.188479          2.895   1.604006
#> 2:     COL  2010           urban     3.065242          2.665   1.681036
#> 3:     COL  2012           rural     3.265382          2.820   1.874445
#> 4:     COL  2012           urban     3.079695          2.770   1.630635
#> 5:     MEX  2010           rural     3.168705          2.755   1.709316
#> 6:     MEX  2010           urban     3.046251          2.710   1.606775
#>    welfare_n    cpi          gdp population
#>        <int>  <num>        <num>      <num>
#> 1:       200 123.61 361603690112   19101893
#> 2:       200  95.98 361603690112   11372838
#> 3:       200  87.10 188367075631    4903047
#> 4:       200 110.23 188367075631   17324540
#> 5:       200 112.86  43774562485    9420639
#> 6:       200 101.51  43774562485    8465146

Save the summary with lineage: parents include all welfare micro datasets plus CPI/GDP/population.

# Only save summary if it has data (i.e., welfare data was available)
if (nrow(summary_table) > 0) {
  parent_paths <- c(
    data_files("data/welfare"),
    "data/macro/cpi.qs2",
    "data/macro/gdp.qs2",
    "data/macro/population.qs2"
  )
  parents <- lapply(parent_paths, function(p) {
    list(path = p, version_id = st_latest(p, alias = NULL))
  })

  st_save(
    summary_table,
    "outputs/welfare_summary.qs2",
    pk = c("country", "year", "reporting_level"),
    parents = parents,
    domain = "summary",
    alias = NULL
  )
  st_lineage("outputs/welfare_summary.qs2", depth = 1, alias = NULL)
} else {
  cat("Summary table is empty; skipping save and lineage demo.\n")
}
#> ✔ Saved [qs2] → '/tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2' @
#>   version 3158826321a4f320
#>   level                                               child_path
#> 1     1 /tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2
#> 2     1 /tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2
#> 3     1 /tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2
#> 4     1 /tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2
#> 5     1 /tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2
#> 6     1 /tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2
#> 7     1 /tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2
#> 8     1 /tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2
#> 9     1 /tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2
#>      child_version
#> 1 3158826321a4f320
#> 2 3158826321a4f320
#> 3 3158826321a4f320
#> 4 3158826321a4f320
#> 5 3158826321a4f320
#> 6 3158826321a4f320
#> 7 3158826321a4f320
#> 8 3158826321a4f320
#> 9 3158826321a4f320
#>                                                                   parent_path
#> 1 /tmp/RtmpbVIVtS/Rbuildaa02dc35a3e/stamp/vignettes/data/welfare/COL_2010.qs2
#> 2 /tmp/RtmpbVIVtS/Rbuildaa02dc35a3e/stamp/vignettes/data/welfare/COL_2012.qs2
#> 3 /tmp/RtmpbVIVtS/Rbuildaa02dc35a3e/stamp/vignettes/data/welfare/MEX_2010.qs2
#> 4 /tmp/RtmpbVIVtS/Rbuildaa02dc35a3e/stamp/vignettes/data/welfare/MEX_2015.qs2
#> 5 /tmp/RtmpbVIVtS/Rbuildaa02dc35a3e/stamp/vignettes/data/welfare/PRY_2011.qs2
#> 6 /tmp/RtmpbVIVtS/Rbuildaa02dc35a3e/stamp/vignettes/data/welfare/PRY_2014.qs2
#> 7        /tmp/RtmpbVIVtS/Rbuildaa02dc35a3e/stamp/vignettes/data/macro/cpi.qs2
#> 8        /tmp/RtmpbVIVtS/Rbuildaa02dc35a3e/stamp/vignettes/data/macro/gdp.qs2
#> 9 /tmp/RtmpbVIVtS/Rbuildaa02dc35a3e/stamp/vignettes/data/macro/population.qs2
#>     parent_version
#> 1 db502d2ae8e7b80e
#> 2 b1dc3d7a82e87efd
#> 3 9b11661e02862916
#> 4 5aaeb0e8b8771f6f
#> 5 b82a0f3ec9595e37
#> 6 1471e74bf4044e4b
#> 7 5de2873364de9333
#> 8 9d1d2f36fd6b1622
#> 9 38351a189b2e146a

Note: to make provenance explicit, pass the producing function as code= so st_save() records the function hash. Below we demonstrate both patterns: foo() loads its own data, while bar() accepts pre-loaded objects and is saved with code = bar.

# Variant that accepts loaded objects (preferred for testability and clarity)
bar <- function(welfare_list, cpi_tbl, gdp_tbl, pop_tbl) {
  if (length(welfare_list) == 0) {
    cat("No welfare data provided; returning empty table.\n")
    return(data.table::data.table())
  }
  wf <- data.table::rbindlist(welfare_list)
  stats <- wf[,
    .(
      welfare_mean = weighted.mean(welfare, weight),
      welfare_median = as.numeric(stats::median(welfare)),
      welfare_sd = stats::sd(welfare),
      welfare_n = .N
    ),
    by = .(country, year, reporting_level)
  ]

  out <- merge(
    stats,
    cpi_tbl,
    by = c("country", "year", "reporting_level"),
    all.x = TRUE
  )
  out <- merge(
    out,
    pop_tbl,
    by = c("country", "year", "reporting_level"),
    all.x = TRUE
  )
  out <- merge(out, gdp_tbl, by = c("country", "year"), all.x = TRUE)
  setcolorder(
    out,
    c(
      "country",
      "year",
      "reporting_level",
      "welfare_mean",
      "welfare_median",
      "welfare_sd",
      "welfare_n",
      "cpi",
      "gdp",
      "population"
    )
  )
  out
}

# Example: load inputs outside the worker and call bar()
welfare_files <- data_files("data/welfare")
if (length(welfare_files) > 0) {
  welfare_list <- lapply(welfare_files, st_load)
  cpi_tbl <- st_load("data/macro/cpi.qs2", alias = NULL)
  gdp_tbl <- st_load("data/macro/gdp.qs2", alias = NULL)
  pop_tbl <- st_load("data/macro/population.qs2", alias = NULL)

  summary_table2 <- bar(welfare_list, cpi_tbl, gdp_tbl, pop_tbl)
  if (nrow(summary_table2) > 0) {
    parent_paths <- c(
      data_files("data/welfare"),
      "data/macro/cpi.qs2",
      "data/macro/gdp.qs2",
      "data/macro/population.qs2"
    )
    parents <- lapply(parent_paths, function(p) {
      list(path = p, version_id = st_latest(p, alias = NULL))
    })
    st_save(
      summary_table2,
      "outputs/welfare_summary.qs2",
      pk = c("country", "year", "reporting_level"),
      parents = parents,
      code = bar, # addition the function as parent so st_save() can track it
      domain = "summary",
      alias = NULL
    )
    st_lineage("outputs/welfare_summary.qs2", depth = 1, alias = NULL)
  } else {
    cat("Summary table2 is empty; skipping save.\n")
  }
} else {
  cat("No welfare data to process in bar-and-save example.\n")
}
#> ✔ Loaded [qs2] ← '/tmp/RtmpCXgvOI/sb561479a059/data/welfare/COL_2010.qs2'
#> ✔ Loaded [qs2] ← '/tmp/RtmpCXgvOI/sb561479a059/data/welfare/COL_2012.qs2'
#> ✔ Loaded [qs2] ← '/tmp/RtmpCXgvOI/sb561479a059/data/welfare/MEX_2010.qs2'
#> ✔ Loaded [qs2] ← '/tmp/RtmpCXgvOI/sb561479a059/data/welfare/MEX_2015.qs2'
#> ✔ Loaded [qs2] ← '/tmp/RtmpCXgvOI/sb561479a059/data/welfare/PRY_2011.qs2'
#> ✔ Loaded [qs2] ← '/tmp/RtmpCXgvOI/sb561479a059/data/welfare/PRY_2014.qs2'
#> ✔ Loaded [qs2] ← '/tmp/RtmpCXgvOI/sb561479a059/data/macro/cpi.qs2'
#> ✔ Loaded [qs2] ← '/tmp/RtmpCXgvOI/sb561479a059/data/macro/gdp.qs2'
#> ✔ Loaded [qs2] ← '/tmp/RtmpCXgvOI/sb561479a059/data/macro/population.qs2'
#> ✔ Saved [qs2] → '/tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2' @
#>   version 30e778dfdc518f1b
#>   level                                               child_path
#> 1     1 /tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2
#> 2     1 /tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2
#> 3     1 /tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2
#> 4     1 /tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2
#> 5     1 /tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2
#> 6     1 /tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2
#> 7     1 /tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2
#> 8     1 /tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2
#> 9     1 /tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2
#>      child_version
#> 1 30e778dfdc518f1b
#> 2 30e778dfdc518f1b
#> 3 30e778dfdc518f1b
#> 4 30e778dfdc518f1b
#> 5 30e778dfdc518f1b
#> 6 30e778dfdc518f1b
#> 7 30e778dfdc518f1b
#> 8 30e778dfdc518f1b
#> 9 30e778dfdc518f1b
#>                                                                   parent_path
#> 1 /tmp/RtmpbVIVtS/Rbuildaa02dc35a3e/stamp/vignettes/data/welfare/COL_2010.qs2
#> 2 /tmp/RtmpbVIVtS/Rbuildaa02dc35a3e/stamp/vignettes/data/welfare/COL_2012.qs2
#> 3 /tmp/RtmpbVIVtS/Rbuildaa02dc35a3e/stamp/vignettes/data/welfare/MEX_2010.qs2
#> 4 /tmp/RtmpbVIVtS/Rbuildaa02dc35a3e/stamp/vignettes/data/welfare/MEX_2015.qs2
#> 5 /tmp/RtmpbVIVtS/Rbuildaa02dc35a3e/stamp/vignettes/data/welfare/PRY_2011.qs2
#> 6 /tmp/RtmpbVIVtS/Rbuildaa02dc35a3e/stamp/vignettes/data/welfare/PRY_2014.qs2
#> 7        /tmp/RtmpbVIVtS/Rbuildaa02dc35a3e/stamp/vignettes/data/macro/cpi.qs2
#> 8        /tmp/RtmpbVIVtS/Rbuildaa02dc35a3e/stamp/vignettes/data/macro/gdp.qs2
#> 9 /tmp/RtmpbVIVtS/Rbuildaa02dc35a3e/stamp/vignettes/data/macro/population.qs2
#>     parent_version
#> 1 db502d2ae8e7b80e
#> 2 b1dc3d7a82e87efd
#> 3 9b11661e02862916
#> 4 5aaeb0e8b8771f6f
#> 5 b82a0f3ec9595e37
#> 6 1471e74bf4044e4b
#> 7 5de2873364de9333
#> 8 9d1d2f36fd6b1622
#> 9 38351a189b2e146a

Handling multiple function dependencies

If the output depends on more than one function (for example foo() calls f1() and f2()), you can provide a combined code metadata to st_save() that captures all producing functions. st_save() accepts any object for code; best practice is to pass a short list of the functions involved, e.g. code = list(foo = foo, helper1 = f1, helper2 = f2). The package hashes code and records a code_hash in the sidecar so downstream st_is_stale() can detect changes in any contributor.

Example pattern:

# Provide multiple functions to st_save so code_hash changes if any of them change
# st_save(x, path, code = list(foo = foo, helper1 = f1, helper2 = f2))

4. Modify CPI (COL 2012) → New Version & Staleness

We change CPI for COL 2012 (all reporting levels) to illustrate a new version and stale downstream artifact.

cpi2 <- st_load("data/macro/cpi.qs2", alias = NULL)
#> ✔ Loaded [qs2] ← '/tmp/RtmpCXgvOI/sb561479a059/data/macro/cpi.qs2'
cpi2[country == "COL" & year == 2012, cpi := cpi * 1.05] # 5% adjustment
st_save(cpi2, "data/macro/cpi.qs2", alias = NULL) # new version recorded
#> ✔ Saved [qs2] → '/tmp/RtmpCXgvOI/sb561479a059/data/macro/cpi.qs2' @ version
#>   5e0c0e8d7f07263c
st_versions("data/macro/cpi.qs2", alias = NULL)[1:3]
#>          version_id      artifact_id     content_hash code_hash size_bytes
#>              <char>           <char>           <char>    <char>      <num>
#> 1: 5e0c0e8d7f07263c 003b5905aba993e3 c19cc5c65b5774c0      <NA>        897
#> 2: 5de2873364de9333 003b5905aba993e3 b678a67494dd3300      <NA>        886
#> 3:             <NA>             <NA>             <NA>      <NA>         NA
#>                     created_at sidecar_format
#>                         <char>         <char>
#> 1: 2026-12-04T20:09:36.534741Z           json
#> 2: 2026-12-04T20:09:35.872195Z           json
#> 3:                        <NA>           <NA>
st_is_stale("outputs/welfare_summary.qs2") # should be TRUE
#> [1] TRUE

Builders, plans and rebuilds — concepts and how we use them here

A short glossary and explanation of the concepts used in the examples above:

  • Artifact: a saved object on disk (one path managed by stamp).

  • Sidecar: metadata file beside each artifact (PKs, parents, code hash, domain, timestamps).

  • Version: immutable snapshot; new version id recorded when st_save() detects changed content.

  • Parent: pointer (path + version_id) to an upstream artifact used to produce a downstream artifact.

  • Lineage: directed graph linking artifacts via parent relationships; inspect with st_lineage().

  • Builder: function registered with st_register_builder(path, builder_fn) that knows how to produce one artifact. Returns at least x (object to save) and code (function(s) used) so code changes trigger staleness.

  • Plan: result of st_plan_rebuild() describing which targets are stale (parents changed, code changed, or artifact missing) and thus need rebuilding.

  • Rebuild: executing a plan via st_rebuild(plan); runs builders, saves artifacts, updates sidecars/versions.

General idea - Register builders for reproducible targets. Builders should be deterministic and return the object to save (do not save directly inside the builder). - Use st_plan_rebuild() to compute what needs to be rebuilt (it inspects recorded parents and code hashes). - Inspect the plan, then call st_rebuild() to execute builders and record new versions.

How this vignette implements those ideas - We register a builder for out_summary_path (see the st_register_builder() call below). That builder returns list(x = foo(), code = foo, code_label = "aggregate_welfare"). - st_plan_rebuild() compares current parent version ids and the code hash of foo against what the sidecar recorded; if any parent or code changed the plan will mark the target as stale. - st_rebuild(plan) calls the registered builder, then st_save() persistently records the new artifact and sidecar (including the new code_hash and parent pointers). - This flow gives you a lightweight, auditable, and incremental pipeline: change an upstream artifact or a function and the plan will tell you what to rebuild.

Practical tips and a small example - Builders should prefer to use the parents argument passed in (this avoids global state and makes builders easier to test). - If your target depends on multiple functions, pass them all in code so st_save() records a combined code hash: code = list(foo = foo, helper = f1).

Example builder that loads parents explicitly and returns multiple functions in code:

# Example: register a builder that uses the provided `parents` list and avoids global loads
summary_path <- fs::path(root_dir, "outputs", "welfare_summary.qs2")
st_register_builder(summary_path, function(path, parents) {
  # parents is a list of lists: each element has $path and $version_id
  # Filter parent paths to identify welfare inputs vs macro inputs, then load them
  welfare_parent_paths <- vapply(
    parents,
    function(p) grepl("welfare", p$path),
    logical(1)
  )
  welfare_list <- lapply(parents[welfare_parent_paths], function(p) {
    st_load(p$path, alias = NULL)
  })

  cpi_tbl <- st_load("data/macro/cpi.qs2", alias = NULL)
  gdp_tbl <- st_load("data/macro/gdp.qs2", alias = NULL)
  pop_tbl <- st_load("data/macro/population.qs2", alias = NULL)

  # Produce the output using a helper function (bar) that accepts pre-loaded inputs
  out <- bar(welfare_list, cpi_tbl, gdp_tbl, pop_tbl)

  # Return the produced object plus code metadata (capture both producer and helpers)
  list(
    x = out,
    code = list(bar = bar), # include helpers as needed
    code_label = "aggregate_welfare"
  )
})
#> ✔ Registered builder for
#>   /tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2 (default)

Inspect the plan before running it:

plan <- st_plan_rebuild(
  targets = "outputs/welfare_summary.qs2",
  include_targets = TRUE,
  mode = "strict"
)
print(plan)
#>   level                        path         reason latest_version_before
#> 1     0 outputs/welfare_summary.qs2 parent_changed      30e778dfdc518f1b

Run the plan to execute the builders and record new versions:

st_rebuild(plan)
#> ✔ Rebuild level 0: 1 artifact
#>   • outputs/welfare_summary.qs2 (parent_changed)
#> Warning: FAILED: ✖ Absolute path
#> '/tmp/RtmpbVIVtS/Rbuildaa02dc35a3e/stamp/vignettes/data/welfare/COL_2010.qs2'
#> is not under alias root. ℹ Alias "default" root: '/tmp/RtmpCXgvOI/sb561479a059'
#> ℹ Provide a relative path or an absolute path under the alias root.
#> ✔ Rebuild summary
#>   failed 1

This example shows the recommended pattern: load from parents, call a pure worker (like bar()), and return x and code. That ensures st_rebuild() can uniformly save artifacts, record lineage, and compute code_hash changes for future incremental rebuilds.

Plan & rebuild using a registered builder for the summary artifact.

st_register_builder(summary_path, function(path, parents) {
  list(x = foo(), code = foo, code_label = "aggregate_welfare")
})
#> ✔ Registered builder for
#>   /tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2 (default)

plan <- st_plan_rebuild(
  targets = "outputs/welfare_summary.qs2",
  include_targets = TRUE,
  mode = "strict"
)
plan
#>   level                        path         reason latest_version_before
#> 1     0 outputs/welfare_summary.qs2 parent_changed      30e778dfdc518f1b
st_rebuild(plan)
#> ✔ Rebuild level 0: 1 artifact
#>   • outputs/welfare_summary.qs2 (parent_changed)
#> ✔ Loaded [qs2] ← '/tmp/RtmpCXgvOI/sb561479a059/data/welfare/COL_2010.qs2'
#> ✔ Loaded [qs2] ← '/tmp/RtmpCXgvOI/sb561479a059/data/welfare/COL_2012.qs2'
#> ✔ Loaded [qs2] ← '/tmp/RtmpCXgvOI/sb561479a059/data/welfare/MEX_2010.qs2'
#> ✔ Loaded [qs2] ← '/tmp/RtmpCXgvOI/sb561479a059/data/welfare/MEX_2015.qs2'
#> ✔ Loaded [qs2] ← '/tmp/RtmpCXgvOI/sb561479a059/data/welfare/PRY_2011.qs2'
#> ✔ Loaded [qs2] ← '/tmp/RtmpCXgvOI/sb561479a059/data/welfare/PRY_2014.qs2'
#> ✔ Loaded [qs2] ← '/tmp/RtmpCXgvOI/sb561479a059/data/macro/cpi.qs2'
#> ✔ Loaded [qs2] ← '/tmp/RtmpCXgvOI/sb561479a059/data/macro/gdp.qs2'
#> ✔ Loaded [qs2] ← '/tmp/RtmpCXgvOI/sb561479a059/data/macro/population.qs2'
#> ✔ Saved [qs2] → '/tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2' @
#>   version c6dad8578d964f8d
#> OK @ version c6dad8578d964f8d
#> ✔ Rebuild summary
#>   built 1
st_lineage("outputs/welfare_summary.qs2", depth = 1, alias = NULL)
#>   level                                               child_path
#> 1     1 /tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2
#> 2     1 /tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2
#> 3     1 /tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2
#> 4     1 /tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2
#> 5     1 /tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2
#> 6     1 /tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2
#> 7     1 /tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2
#> 8     1 /tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2
#> 9     1 /tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2
#>      child_version
#> 1 c6dad8578d964f8d
#> 2 c6dad8578d964f8d
#> 3 c6dad8578d964f8d
#> 4 c6dad8578d964f8d
#> 5 c6dad8578d964f8d
#> 6 c6dad8578d964f8d
#> 7 c6dad8578d964f8d
#> 8 c6dad8578d964f8d
#> 9 c6dad8578d964f8d
#>                                                                   parent_path
#> 1 /tmp/RtmpbVIVtS/Rbuildaa02dc35a3e/stamp/vignettes/data/welfare/COL_2010.qs2
#> 2 /tmp/RtmpbVIVtS/Rbuildaa02dc35a3e/stamp/vignettes/data/welfare/COL_2012.qs2
#> 3 /tmp/RtmpbVIVtS/Rbuildaa02dc35a3e/stamp/vignettes/data/welfare/MEX_2010.qs2
#> 4 /tmp/RtmpbVIVtS/Rbuildaa02dc35a3e/stamp/vignettes/data/welfare/MEX_2015.qs2
#> 5 /tmp/RtmpbVIVtS/Rbuildaa02dc35a3e/stamp/vignettes/data/welfare/PRY_2011.qs2
#> 6 /tmp/RtmpbVIVtS/Rbuildaa02dc35a3e/stamp/vignettes/data/welfare/PRY_2014.qs2
#> 7        /tmp/RtmpbVIVtS/Rbuildaa02dc35a3e/stamp/vignettes/data/macro/cpi.qs2
#> 8        /tmp/RtmpbVIVtS/Rbuildaa02dc35a3e/stamp/vignettes/data/macro/gdp.qs2
#> 9 /tmp/RtmpbVIVtS/Rbuildaa02dc35a3e/stamp/vignettes/data/macro/population.qs2
#>     parent_version
#> 1 db502d2ae8e7b80e
#> 2 b1dc3d7a82e87efd
#> 3 9b11661e02862916
#> 4 5aaeb0e8b8771f6f
#> 5 b82a0f3ec9595e37
#> 6 1471e74bf4044e4b
#> 7 5e0c0e8d7f07263c
#> 8 9d1d2f36fd6b1622
#> 9 38351a189b2e146a

5. Add New Data (ARG 2015 Welfare)

We introduce a new welfare dataset (ARG 2015) which appears in macro tables already. Only the summary rows for ARG 2015 will be newly added.

dt_arg_2015 <- simulate_welfare("ARG", 2015)
st_save(
  dt_arg_2015,
  "data/welfare/ARG_2015.qs2",
  pk = c("country", "year", "reporting_level", "hh_id"),
  domain = "welfare",
  alias = NULL
)
#> ✔ Saved [qs2] → '/tmp/RtmpCXgvOI/sb561479a059/data/welfare/ARG_2015.qs2' @
#>   version a7207873b0f1c757

# Only recompute and save summary if we have welfare data
if (length(data_files("data/welfare")) > 0 && nrow(summary_table) > 0) {
  parents2 <- lapply(
    c(
      data_files("data/welfare"),
      "data/macro/cpi.qs2",
      "data/macro/gdp.qs2",
      "data/macro/population.qs2"
    ),
    function(p) list(path = p, version_id = st_latest(p, alias = NULL))
  )
  summary_table_new <- foo()
  if (nrow(summary_table_new) > 0) {
    st_save(
      summary_table_new,
      "outputs/welfare_summary.qs2",
      pk = c("country", "year", "reporting_level"),
      parents = parents2,
      domain = "summary",
      alias = NULL
    )
    head(st_load("outputs/welfare_summary.qs2", alias = NULL))
  } else {
    cat("Summary table after adding ARG 2015 is empty; skipping save.\n")
  }
} else {
  cat("Insufficient data to recompute summary.\n")
}
#> ✔ Loaded [qs2] ← '/tmp/RtmpCXgvOI/sb561479a059/data/welfare/ARG_2015.qs2'
#> ✔ Loaded [qs2] ← '/tmp/RtmpCXgvOI/sb561479a059/data/welfare/COL_2010.qs2'
#> ✔ Loaded [qs2] ← '/tmp/RtmpCXgvOI/sb561479a059/data/welfare/COL_2012.qs2'
#> ✔ Loaded [qs2] ← '/tmp/RtmpCXgvOI/sb561479a059/data/welfare/MEX_2010.qs2'
#> ✔ Loaded [qs2] ← '/tmp/RtmpCXgvOI/sb561479a059/data/welfare/MEX_2015.qs2'
#> ✔ Loaded [qs2] ← '/tmp/RtmpCXgvOI/sb561479a059/data/welfare/PRY_2011.qs2'
#> ✔ Loaded [qs2] ← '/tmp/RtmpCXgvOI/sb561479a059/data/welfare/PRY_2014.qs2'
#> ✔ Loaded [qs2] ← '/tmp/RtmpCXgvOI/sb561479a059/data/macro/cpi.qs2'
#> ✔ Loaded [qs2] ← '/tmp/RtmpCXgvOI/sb561479a059/data/macro/gdp.qs2'
#> ✔ Loaded [qs2] ← '/tmp/RtmpCXgvOI/sb561479a059/data/macro/population.qs2'
#> ✔ Saved [qs2] → '/tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2' @
#>   version d63f73f63a6f2a04
#> ✔ Loaded [qs2] ← '/tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2'
#> Key: <country, year>
#>    country  year reporting_level welfare_mean welfare_median welfare_sd
#>     <char> <int>          <char>        <num>          <num>      <num>
#> 1:     ARG  2015        national     3.150381          2.615   1.724803
#> 2:     COL  2010           rural     3.188479          2.895   1.604006
#> 3:     COL  2010           urban     3.065242          2.665   1.681036
#> 4:     COL  2012           rural     3.265382          2.820   1.874445
#> 5:     COL  2012           urban     3.079695          2.770   1.630635
#> 6:     MEX  2010           rural     3.168705          2.755   1.709316
#>    welfare_n      cpi          gdp population
#>        <int>    <num>        <num>      <num>
#> 1:       200 114.9600   3903279160   12517202
#> 2:       200 123.6100 361603690112   19101893
#> 3:       200  95.9800 361603690112   11372838
#> 4:       200  91.4550 188367075631    4903047
#> 5:       200 115.7415 188367075631   17324540
#> 6:       200 112.8600  43774562485    9420639

6. Summary

We demonstrated:

  • Saving multiple artifacts with PK metadata and automatic versioning.
  • Building a derived summary with explicit lineage and rebuilding after an upstream change.
  • Adding new data (ARG 2015) and incorporating it with minimal friction.

Explore lineage further:

st_lineage("outputs/welfare_summary.qs2", depth = 2, alias = NULL)[1:10, ]
#>    level                                               child_path
#> 1      1 /tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2
#> 2      1 /tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2
#> 3      1 /tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2
#> 4      1 /tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2
#> 5      1 /tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2
#> 6      1 /tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2
#> 7      1 /tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2
#> 8      1 /tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2
#> 9      1 /tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2
#> 10     1 /tmp/RtmpCXgvOI/sb561479a059/outputs/welfare_summary.qs2
#>       child_version
#> 1  d63f73f63a6f2a04
#> 2  d63f73f63a6f2a04
#> 3  d63f73f63a6f2a04
#> 4  d63f73f63a6f2a04
#> 5  d63f73f63a6f2a04
#> 6  d63f73f63a6f2a04
#> 7  d63f73f63a6f2a04
#> 8  d63f73f63a6f2a04
#> 9  d63f73f63a6f2a04
#> 10 d63f73f63a6f2a04
#>                                                                    parent_path
#> 1  /tmp/RtmpbVIVtS/Rbuildaa02dc35a3e/stamp/vignettes/data/welfare/ARG_2015.qs2
#> 2  /tmp/RtmpbVIVtS/Rbuildaa02dc35a3e/stamp/vignettes/data/welfare/COL_2010.qs2
#> 3  /tmp/RtmpbVIVtS/Rbuildaa02dc35a3e/stamp/vignettes/data/welfare/COL_2012.qs2
#> 4  /tmp/RtmpbVIVtS/Rbuildaa02dc35a3e/stamp/vignettes/data/welfare/MEX_2010.qs2
#> 5  /tmp/RtmpbVIVtS/Rbuildaa02dc35a3e/stamp/vignettes/data/welfare/MEX_2015.qs2
#> 6  /tmp/RtmpbVIVtS/Rbuildaa02dc35a3e/stamp/vignettes/data/welfare/PRY_2011.qs2
#> 7  /tmp/RtmpbVIVtS/Rbuildaa02dc35a3e/stamp/vignettes/data/welfare/PRY_2014.qs2
#> 8         /tmp/RtmpbVIVtS/Rbuildaa02dc35a3e/stamp/vignettes/data/macro/cpi.qs2
#> 9         /tmp/RtmpbVIVtS/Rbuildaa02dc35a3e/stamp/vignettes/data/macro/gdp.qs2
#> 10 /tmp/RtmpbVIVtS/Rbuildaa02dc35a3e/stamp/vignettes/data/macro/population.qs2
#>      parent_version
#> 1  a7207873b0f1c757
#> 2  db502d2ae8e7b80e
#> 3  b1dc3d7a82e87efd
#> 4  9b11661e02862916
#> 5  5aaeb0e8b8771f6f
#> 6  b82a0f3ec9595e37
#> 7  1471e74bf4044e4b
#> 8  5e0c0e8d7f07263c
#> 9  9d1d2f36fd6b1622
#> 10 38351a189b2e146a