set_meta: Attach metadata to a dataset

View source: R/meta.R

set_metaR Documentation

Attach metadata to a dataset

Description

Stamp a artoo_meta onto a data frame as a single Dataset-JSON string in its metadata_json attribute. Every ⁠write_*()⁠ codec reads that string back with get_meta() and embeds it verbatim, so the metadata survives the trip to any format. Use it to attach metadata to a bare frame before a write, or to re-stamp after a tidyverse verb has dropped attributes.

Usage

set_meta(x, meta)

Arguments

x

The data frame to stamp. ⁠<data.frame>: required⁠.

meta

The metadata to attach. ⁠<artoo_meta>: required⁠. Usually from get_meta() or built by apply_spec().

Value

The data frame x, with its metadata_json attribute set. Pass it on to a ⁠write_*()⁠ codec or back through get_meta().

See Also

get_meta() for the read half; apply_spec() which stamps it.

Examples

# ---- Example 1: re-stamp metadata a dplyr verb would drop ----
#
# Conform a dataset, capture its metadata, then re-attach after an
# attribute-dropping transform so the write stays lossless.
spec <- artoo_spec(
  cdisc_adam_datasets, cdisc_adam_variables,
  codelists = cdisc_codelists
)
adsl <- apply_spec(cdisc_adsl, spec, "ADSL")
meta <- get_meta(adsl)
trimmed <- head(as.data.frame(adsl), 5)
attr(trimmed, "metadata_json") <- NULL
set_meta(trimmed, meta)

# ---- Example 2: borrow metadata from a conformed dataset ----
#
# A writer with a raw frame can lift metadata off a conformed dataset and
# stamp it onto the bare frame (DM is SDTM, so its spec is sdtm-shaped).
sdtm <- artoo_spec(
  cdisc_sdtm_datasets, cdisc_sdtm_variables,
  codelists = cdisc_codelists
)
meta_dm <- get_meta(apply_spec(cdisc_dm, sdtm, "DM"))
dm <- set_meta(cdisc_dm, meta_dm)
is_artoo_meta(get_meta(dm))


artoo documentation built on July 23, 2026, 1:08 a.m.