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

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

Arguments

x

A tibble-like object

...

Passed on to structure()

nrow

The number of rows, guessed from the data by default

subclass

Subclasses to assign to the new object, default: none

Details

x must be a named (or empty) list, but the names are not currently checked for correctness.

The ... argument allows adding more attributes to the subclass.

The row.names attribute will be computed from the nrow argument, overriding any existing attribute of this name in x or in the ... arguments. If nrow is NULL, the number of rows will be guessed from the data. The new_tibble() constructor makes sure that the row.names attribute is consistent with the data before returning.

The class attribute of the returned object always consists of c("tbl_df", "tbl", "data.frame"). If the subclass argument is set, it will be prepended to that list of classes.

Examples

new_tibble(list(a = 1:3, b = 4:6))
#> # A tibble: 3 x 2 #> a b #> <int> <int> #> 1 1 4 #> 2 2 5 #> 3 3 6
# One particular situation where the nrow argument is essential: new_tibble(list(), nrow = 150, subclass = "my_tibble")
#> # A tibble: 150 x 0
# It's safest to always pass it along: 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
# NOT RUN { # All columns must be the same length: new_tibble(list(a = 1:3, b = 4.6)) # The length must be consistent with the nrow argument if available: new_tibble(list(a = 1:3, b = 4:6), nrow = 2) # }