Eample 5: Tibble

tibble is the name of a package, as well as a enhanced data.frame structure, which provides user lots of information automatically. I admire this design, and used to think of adding as_tibble to every end of function. This is, however, inefficient for data manipulation, which violates the principal of “fast”. But thanks to Houyun, he taught me a way to adjust the print.data.frame function, which I finally use for pring.data.table for an alternative way of printing data.table. Now let’s try it with the function show_tibble:

library(tidyfst)

iris %>% count_dt(Species) -> a
class(a)
#> [1] "data.table" "data.frame"

# turn on
print.data.table = show_tibble(TRUE)
#> The tibble mode has been turned on.
a
#>       Species  n
#> 1:     setosa 50
#> 2: versicolor 50
#> 3:  virginica 50
class(a)
#> [1] "data.table" "data.frame"

# shut off
print.data.table = show_tibble(FALSE)
#> The tibble mode has been shut off.
a
#>       Species  n
#> 1:     setosa 50
#> 2: versicolor 50
#> 3:  virginica 50
class(a)
#> [1] "data.table" "data.frame"

We can find that the structure of variable a never changes, but the printing form of it could be altered to tibble or back to data.table using show_tibble function. You can choose the one you preferred.