mutate_annotation: Modify dataset variable annotations

View source: R/annotating-mutate.R

mutate_annotationR Documentation

Modify dataset variable annotations

Description

Usually, metadata should be a reflection of what the data should represent and act as a check on the generation code. However, in the course of data aggregation, it can be common to perform massive transformations that would be cumbersome to document manually. This exposes a metadata-manipulation framework prior to metadata file creation, in the style of tidytable::mutate.

Usage

mutate_annotation(.data, .field, ..., .overwrite = TRUE)

mutate_annotation_across(
  .data,
  .field,
  .fn,
  .cols = tidyselect::everything(),
  .with_names = FALSE,
  ...,
  .overwrite = TRUE
)

Arguments

.data

A data.frame

.field

The name of the annotation field that you wish to modify

...

For mutate_annotation, named parameters that contain the annotation values. Like tidytable::mutate, each parameter name is a variable (that must already exist!), and each parameter value is an R expression, evaluated with .data as a data mask.

For mutate_annotation_across, extra arguments passed to .fn

.overwrite

If TRUE, overwrites existing annotation values. Annotations have an overwriting guard by default, but since these functions are intentionally modifying the annotations, this parameter defaults to TRUE.

.fn

A function that takes in a vector and arbitrary arguments ... If .with_names is TRUE, then .fn will be passed the vector and the name of the vector, since it's often useful to compute on the metadata.

.cols

A tidyselect-compatible selection of variables to be edited

.with_names

If TRUE, passes a column and its name as arguments to .fn

Value

A data.frame with annotated columns

Examples

# Adds a "mean" annotation to 'mpg'
mutate_annotation(mtcars, "mean", mpg = mean(mpg))

# Adds a "mean" annotation to all variables in `mtcars`
mutate_annotation_across(mtcars, "mean", .fn = mean)

# Adds a "title" annotation that copies the column name
mutate_annotation_across(
  mtcars,
  "title",
  .fn = function(x, nx) nx,
  .with_names = TRUE
)

nyuglobalties/blueprintr documentation built on July 16, 2024, 10:27 a.m.