Maturing lifecycle

Creates or validates a subclass of a tibble. These function is mostly useful for package authors that implement subclasses of a tibble, like sf or tsibble.

new_tibble() creates a new object as a subclass of tbl_df, tbl and data.frame. This function is optimized for performance, checks are reduced to a minimum.

validate_tibble() checks a tibble for internal consistency. Correct behavior can be guaranteed only if this function runs without raising an error.

new_tibble(x, ..., nrow, class = NULL, subclass = NULL)

validate_tibble(x)

Arguments

x

A tibble-like object

...

Passed on to structure()

nrow

The number of rows, required

class

Subclasses to assign to the new object, default: none

subclass

Deprecated, retained for compatibility. Please use the class argument.

Construction

For new_tibble(), x must be a list. The ... argument allows adding more attributes to the subclass. An nrow argument is required. This should be an integer of length 1, and every element of the list x should have NROW() equal to this value. (But this is not checked by the constructor). This takes the place of the "row.names" attribute in a data frame. x must have names (or be empty), but the names are not checked for correctness.

Validation

validate_tibble() checks for "minimal" names and that all columns are vectors, data frames or matrices. It also makes sure that all columns have the same length, and that NROW() is consistent with the data. 1d arrays are not supported.

See also

tibble() and as_tibble() for ways to construct a tibble with recycling of scalars and automatic name repair.

Examples

# The nrow argument is always required: new_tibble(list(a = 1:3, b = 4:6), nrow = 3)
#> # A tibble: 3 x 2 #> a b #> <int> <int> #> 1 1 4 #> 2 2 5 #> 3 3 6
# Existing row.names attributes are ignored: try(new_tibble(iris, nrow = 3))
#> # A tibble: 3 x 5 #> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> <dbl> <dbl> <dbl> <dbl> <fct> #> 1 5.1 3.5 1.4 0.2 setosa #> 2 4.9 3 1.4 0.2 setosa #> 3 4.7 3.2 1.3 0.2 setosa
# The length of all columns must be consistent with the nrow argument: try(new_tibble(list(a = 1:3, b = 4:6), nrow = 2))
#> # A tibble: 2 x 2 #> a b #> <int> <int> #> 1 1 4 #> 2 2 5