This error occurs when a function expects a vector and gets a scalar object instead. This commonly happens when some code attempts to assign a scalar object as column in a data frame:

fn <- function() NULL
tibble::tibble(x = fn)

fit <- lm(1:3 ~ 1)
tibble::tibble(x = fit)

Vectorness in base R and in the tidyverse

In base R, almost everything is a vector or behaves like a vector. In the tidyverse we have chosen to be a bit stricter about what is considered a vector. The main question we ask ourselves to decide on the vectorness of a type is whether it makes sense to include that object as a column in a data frame.

The main difference is that S3 lists are considered vectors by base R but in the tidyverse that's not the case by default:

fit <- lm(1:3 ~ 1)

typeof(fit)
class(fit)

# S3 lists can be subset like a vector using base R:
fit[c(1, 4)]

# But not in vctrs
vctrs::vec_slice(fit, c(1, 4))

Defused function calls are another (more esoteric) example:

call <- quote(foo(bar = TRUE, baz = FALSE))
call

# They can be subset like a vector using base R:
call[1:2]
lapply(call, function(x) x)

# But not with vctrs:
vctrs::vec_slice(call, 1:2)

I get a scalar type error but I think this is a bug

It's possible the author of the class needs to do some work to declare their class a vector. Consider reaching out to the author. We have written a [developer FAQ page][howto-faq-fix-scalar-type-error] to help them fix the issue.



Try the vctrs package in your browser

Any scripts or data that you put into this service are public.

vctrs documentation built on Oct. 13, 2023, 1:05 a.m.