https://github.com/krlmlr/scalar
  
  
    Scalars 
    https://github.com/krlmlr/scalar
  
r
        Last synced: 3 months ago 
        JSON representation
    
Scalars
- Host: GitHub
 - URL: https://github.com/krlmlr/scalar
 - Owner: krlmlr
 - Archived: true
 - Created: 2019-10-27T17:34:36.000Z (about 6 years ago)
 - Default Branch: master
 - Last Pushed: 2019-10-27T17:34:52.000Z (about 6 years ago)
 - Last Synced: 2024-12-04T09:40:05.180Z (11 months ago)
 - Topics: r
 - Language: R
 - Size: 6.84 KB
 - Stars: 1
 - Watchers: 2
 - Forks: 0
 - Open Issues: 0
 - 
            Metadata Files:
            
- Readme: README.Rmd
 
 
Awesome Lists containing this project
- jimsghstars - krlmlr/scalar - Scalars (R)
 
README
          ---
output: github_document
---
```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)
library(tidyverse)
pkgload::load_all()
```
# scalar
[](https://www.tidyverse.org/lifecycle/#experimental)
The goal of scalar is to provide a [vctrs](https://vctrs.r-lib.org/) compatible way to indicate that a vector is intended to have length one.
## Installation
You can install the released version of scalar from [CRAN](https://CRAN.R-project.org) with:
``` r
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.
```{r example}
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.
```{r example2, error = TRUE}
scalar(1:2)
vec_c(scalar(1), scalar(2))
```