knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)
library(tidyverse)
pkgload::load_all()

scalar

Lifecycle: experimental

The goal of scalar is to provide a vctrs compatible way to indicate that a vector is intended to have length one.

Installation

You can install the released version of scalar from CRAN with:

install.packages("scalar")

Example

This class is useful to distinguish between scalars and vectors when creating one-row tibbles that contain arbitrary objects, e.g. results of rowwise()-like computations.

library(scalar)

wrap_unless_scalar <- function(x) {
  if (is_scalar(x)) return(x)
  list(x)
}

tibble_row <- function(...) {
  lst <- tibble::lst(...)
  row <- purrr::map(lst, wrap_unless_scalar)
  tibble(!!!row)
}

tibble_row(a = 1, b = 2:3, c = list(4))
tibble_row(a = scalar(1), b = 2:3, c = scalar(list(4)))

Non-scalars are rejected. Concatenation returns a "bare" object.

scalar(1:2)
vec_c(scalar(1), scalar(2))


krlmlr/scalar documentation built on Nov. 4, 2019, 3:57 p.m.