| sync_meta | R Documentation |
Re-attach and reconcile a artoo_meta after a transformation that dropped
or reshaped it: the metadata's columns are narrowed and reordered to the
frame's current columns, the record count is refreshed, the keys are
recomputed, and a column the metadata does not describe gets an entry
synthesized from its class and attributes. The one-liner to run after a
dplyr (or base) pipeline, before handing the frame to a write_*() codec.
sync_meta(x, meta = NULL)
x |
The transformed data frame. |
meta |
The metadata to reconcile against. Requirement: when the transform dropped the attribute (base |
Why it exists. Base row subsetting (x[i, ]) drops the frame's
metadata_json attribute, and many tidyverse verbs rebuild the frame.
sync_meta() takes the last-known metadata (the frame's own attribute
when it survived, or an explicit meta) and makes it agree with the data
again, so the round trip stays lossless without hand-editing.
A <data.frame>: x re-stamped with the reconciled
artoo_meta. Hand it to any write_*() codec.
Read / attach: get_meta(), set_meta().
Produce conformed frames: apply_spec().
spec <- artoo_spec(cdisc_adam_datasets, cdisc_adam_variables, codelists = cdisc_codelists)
# ---- Example 1: re-attach after an attribute-dropping subset ----
#
# Base subsetting drops the metadata; capture it first, transform, then
# sync. The metadata narrows to the kept columns and the new row count.
adsl <- apply_spec(cdisc_adsl, spec, "ADSL", conformance = "off")
meta <- get_meta(adsl)
elderly <- adsl[adsl$AGE > 65, c("STUDYID", "USUBJID", "AGE")]
synced <- sync_meta(elderly, meta)
get_meta(synced)@dataset$records
# ---- Example 2: a derived column gains a synthesized entry ----
#
# A new column the metadata does not describe is profiled from its class,
# so the frame still writes losslessly.
adsl$AGEGR9 <- ifelse(adsl$AGE > 65, ">65", "<=65")
synced2 <- sync_meta(adsl)
get_meta(synced2)@columns$AGEGR9$dataType
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.