4.1 Loading specific versions
The version argument in st_load() allows
you to load historical versions of artifacts. This is useful for
comparing changes over time or recovering from mistakes.
# Ensure versioning is enabled for this example
st_opts(versioning = "timestamp") # Force version on every save
## ✔ stamp options updated
## versioning = "timestamp"
# Create multiple versions by modifying and saving
v_path <- "versioned.qs2"
# Version 1
v1 <- data.frame(x = 1:3, y = c("a", "b", "c"))
st_save(v1, v_path, code_label = "initial", alias = NULL)
## ✔ Saved [qs2] → '/tmp/RtmpCXgvOI/stamp-vignette-b566fc37989/versioned.qs2' @
## version 1edb20ad801d17de
Sys.sleep(1.1) # ensure distinct timestamps on all platforms
# Version 2
v2 <- data.frame(x = 1:5, y = c("a", "b", "c", "d", "e"))
st_save(v2, v_path, code_label = "added rows", alias = NULL)
## ✔ Saved [qs2] → '/tmp/RtmpCXgvOI/stamp-vignette-b566fc37989/versioned.qs2' @
## version 2ececce33aed26d7
Sys.sleep(1.1)
# Version 3
v3 <- data.frame(x = 1:5, y = c("a", "b", "c", "d", "e"), z = 10:14)
st_save(v3, v_path, code_label = "added column z", alias = NULL)
## ✔ Saved [qs2] → '/tmp/RtmpCXgvOI/stamp-vignette-b566fc37989/versioned.qs2' @
## version 84556088b0c254de
# Check available versions (explicit alias auto-detect)
versions <- st_versions(v_path, alias = NULL)
if (nrow(versions) == 0) {
cat("Warning: No versions were created. Skipping version loading examples.\n")
} else {
print(versions[, .(version_id, created_at, size_bytes)])
# Load latest (default)
current <- st_load(v_path, alias = NULL)
cat(sprintf("Current: %d rows, %d columns\n", nrow(current), ncol(current)))
# Load previous version (version = -1) when available; skip if snapshot missing
if (nrow(versions) >= 2) {
prev_ok <- TRUE
previous <- tryCatch(
st_load(v_path, version = -1, alias = NULL),
error = function(e) { prev_ok <<- FALSE; NULL }
)
if (isTRUE(prev_ok)) {
cat(sprintf("Previous (v-1): %d rows, %d columns\n", nrow(previous), ncol(previous)))
} else {
cat("Previous (v-1): snapshot not available; skipping.\n")
}
}
}
## version_id created_at size_bytes
## <char> <char> <num>
## 1: 84556088b0c254de 2026-12-04T20:09:31.203453Z 287
## 2: 2ececce33aed26d7 2026-12-04T20:09:30.062508Z 262
## 3: 1edb20ad801d17de 2026-12-04T20:09:28.925050Z 261
## Warning: No primary key recorded for
## '/tmp/RtmpCXgvOI/stamp-vignette-b566fc37989/versioned.qs2'.
## ℹ You can add one with `st_add_pk()`.
## ✔ Loaded [qs2] ← '/tmp/RtmpCXgvOI/stamp-vignette-b566fc37989/versioned.qs2'
## Current: 5 rows, 3 columns
## ✔ Loaded ← versioned.qs2 @ 2ececce33aed26d7 [qs2]
## Previous (v-1): 5 rows, 2 columns
# Load two versions back (version = -2) when available, else oldest by ID
older_ok <- TRUE
if (nrow(versions) >= 3) {
older <- tryCatch(
st_load(v_path, version = -2, alias = NULL),
error = function(e) { older_ok <<- FALSE; NULL }
)
} else if (nrow(versions) >= 1) {
oldest_id <- versions$version_id[nrow(versions)]
older <- tryCatch(
st_load(v_path, version = oldest_id, alias = NULL),
error = function(e) { older_ok <<- FALSE; NULL }
)
} else {
older_ok <- FALSE
older <- NULL
}
## ✔ Loaded ← versioned.qs2 @ 1edb20ad801d17de [qs2]
if (isTRUE(older_ok)) {
cat(sprintf("Older: %d rows, %d columns\n", nrow(older), ncol(older)))
} else {
cat("Older: snapshot not available; skipping.\n")
}
## Older: 3 rows, 2 columns
# Load specific version by ID (oldest)
vid <- versions$version_id[nrow(versions)]
spec_ok <- TRUE
specific <- tryCatch(
st_load(v_path, version = vid, alias = NULL),
error = function(e) { spec_ok <<- FALSE; NULL }
)
## ✔ Loaded ← versioned.qs2 @ 1edb20ad801d17de [qs2]
if (isTRUE(spec_ok) && isTRUE(older_ok)) {
cat(sprintf("Specific version matches older: %s\n", identical(specific, older)))
}
## Specific version matches older: TRUE
# Reset to default versioning
st_opts(versioning = "content")
## ✔ stamp options updated
## versioning = "content"
The version argument supports several modes:
NULL (default): loads the current artifact file
0: same as NULL, loads latest version
- Negative integers (
-1, -2, etc.): load
versions relative to latest
- Character string: specific version ID (from
st_versions())
"select", "pick", or
"choose": interactive menu (in interactive sessions)
# Interactive menu to choose a version (only works in interactive R sessions)
# This will display a menu with formatted timestamps and file sizes
selected <- st_load(v_path, version = "select")
# The menu looks like:
# ℹ Select a version to load from versioned.qs2:
# Latest version is [1]
#
# Available versions:
#
# 1: [1] 2025-11-26 10:30:15 (0.12 MB) - 20251126T103015Z
# 2: [2] 2025-11-26 10:30:10 (0.10 MB) - 20251126T103010Z
# 3: [3] 2025-11-26 10:30:05 (0.08 MB) - 20251126T103005Z
#
# Selection: _