check_pivot_spec: Check assumptions about a pivot 'spec'

View source: R/pivot.R

check_pivot_specR Documentation

Check assumptions about a pivot spec

Description

check_pivot_spec() is a developer facing helper function for validating the pivot spec used in pivot_longer_spec() or pivot_wider_spec(). It is only useful if you are extending pivot_longer() or pivot_wider() with new S3 methods.

check_pivot_spec() makes the following assertions:

  • spec must be a data frame.

  • spec must have a character column named .name.

  • spec must have a character column named .value.

  • The .name column must be unique.

  • The .name and .value columns must be the first two columns in the data frame, and will be reordered if that is not true.

Usage

check_pivot_spec(spec, call = caller_env())

Arguments

spec

A specification data frame. This is useful for more complex pivots because it gives you greater control on how metadata stored in the columns become column names in the result.

Must be a data frame containing character .name and .value columns. Additional columns in spec should be named to match columns in the long format of the dataset and contain values corresponding to columns pivoted from the wide format. The special .seq variable is used to disambiguate rows internally; it is automatically removed after pivoting.

Examples

# A valid spec
spec <- tibble(.name = "a", .value = "b", foo = 1)
check_pivot_spec(spec)

spec <- tibble(.name = "a")
try(check_pivot_spec(spec))

# `.name` and `.value` are forced to be the first two columns
spec <- tibble(foo = 1, .value = "b", .name = "a")
check_pivot_spec(spec)

tidyverse/tidyr documentation built on Oct. 30, 2024, 1:53 a.m.