R tibble package: How to print first 5 and last 5 rows of a tibble (like data.table does)

Viewed 385

I am a longtime data.table user, but my team uses the tidyverse packages so I'm trying to adapt to using tibbles instead of data.tables.

I prefer the data.table version of printing data.frame-like objects in the console, and I'd like to customize the tibble printing to match that version.

library(data.table)
library(tidyverse)
library(stringr)
library(stringi)

d = data.table(x = rnorm(100,5, 1),
               y = rlnorm(100,0,1),
               z = seq(100),
               char = sample(LETTERS, size = 100, replace = TRUE),
               long_char = stringi::stri_rand_strings(100, 30),
               large_num = rnorm(100,5,1)*1e5,
               words = sample(stringr::fruit, size = 100, replace = TRUE),
               sent = sample(stringr::sentences, size = 100, replace = TRUE),
               more_words = stringr::words[1:100],
               x2 = runif(100, 1, 10000),
               y2 = runif(100, log(1), log(10000))
)

here is the simple shortened default version of data.table print which is my desired general format:

> print(d, n=10)
            x         y   z char                      long_char large_num      words
  1: 4.165643 0.5639942   1    H 8Ol63ZRclwzzvMUSVWK5sgiE4DhVKI  601011.4      lemon
  2: 5.388253 1.5795067   2    O lxIqYtz5whHlZAfpfnDgchVyaILhla  673867.5       lime
  3: 4.921814 2.2797885   3    A KBA8X6GtVA3zDE56x80GANVZw2DZ6f  606844.2  mandarine
  4: 6.205763 0.5870983   4    G 0QGlqx64fX9dC7DTEzT1nZzbODrb1g  479568.8   eggplant
  5: 6.428971 0.6681059   5    F fdrg1laPXZT5uxr4lcXFkw1P0gVZxf  551545.1     loquat
 ---                                                                                
 96: 5.585625 1.5754623  96    W AsYwsKJVVMrnKxG2lRlKT1FzC5HjE4  570620.9  tamarillo
 97: 3.428393 3.1993692  97    K H1tMqyKdRChYKxJpb2hPbkqrJPKnJr  454934.9 watermelon
 98: 5.099580 0.2643802  98    E eN9zw4LjUa9gYMWIpTxS6ngqj98zRi  454279.5 elderberry
 99: 4.564551 3.8636661  99    Z Dd2GXl46Z8DwliYi0V5MfqcEcihSzP  606515.5 ugli fruit
100: 4.769761 0.1467731 100    Z X5GeuFnCqnT3Zs2wtVhUtCQ8SQNvBf  468791.4     pomelo
                                              sent more_words        x2         y2
  1:   The box was thrown beside the parked truck.          a 3258.8234 3.46348484
  2: He knew the skill of the great young actress.       able 8089.5246 1.73877279
  3:        Gray paint stretched for miles around.      about  478.9192 1.33255002
  4:           The fur of cats goes by many names.   absolute 4269.9829 8.26232550
  5:                 Pure bred poodles have curls.     accept  707.2006 5.75561335
 ---                                                                              
 96:     Sell your gift to a buyer at a good gain.      bloke 4702.1580 4.75107162
 97:    Screw the round cap on as tight as needed.      blood 2353.0794 6.44434982
 98:           The poor boy missed the boat again.       blow 3006.5165 7.23329206
 99:       The gloss on top made it unfit to read.       blue 1470.8847 0.07777193
100:            Lush fern grow on the lofty rocks.      board 1572.8455 8.79821522

Notice that it prints the first 5 and last 5 rows and that it does not shorten the contents of a column to prevent wrapping across multiple lines.

Now here is the tibble default version:

> # the tibble default
> # notice it only prints the first 10 rows
> # also it shortens the contents of a row so that it doesn't wrap into multiple lines 
> # ^ do not want this
> as_tibble(d)
# A tibble: 100 × 11
       x     y     z char  long_char         large_num words   sent               more_words    x2    y2
   <dbl> <dbl> <int> <chr> <chr>                 <dbl> <chr>   <chr>              <chr>      <dbl> <dbl>
 1  4.17 0.564     1 H     8Ol63ZRclwzzvMUS…   601011. lemon   The box was throw… a          3259. 3.46 
 2  5.39 1.58      2 O     lxIqYtz5whHlZAfp…   673867. lime    He knew the skill… able       8090. 1.74 
 3  4.92 2.28      3 A     KBA8X6GtVA3zDE56…   606844. mandar… Gray paint stretc… about       479. 1.33 
 4  6.21 0.587     4 G     0QGlqx64fX9dC7DT…   479569. eggpla… The fur of cats g… absolute   4270. 8.26 
 5  6.43 0.668     5 F     fdrg1laPXZT5uxr4…   551545. loquat  Pure bred poodles… accept      707. 5.76 
 6  5.28 2.44      6 J     aqhW8bHx8YdYPT8m…   402228. pineap… A dash of pepper … account    5685. 1.61 
 7  4.25 0.316     7 L     u5euDWP0iHDaHqeI…   587085. blackb… Dig deep in the e… achieve    7315. 0.257
 8  5.13 0.603     8 V     76yqXz8vtd1C0ew1…   578124. cucumb… The child crawled… across     1384. 0.613
 9  3.85 2.70      9 B     kxQWSZcveGPulgub…   548137. strawb… Sell your gift to… act        6796. 1.46 
10  6.04 1.77     10 V     ZbFmoGsqd65FPKHI…   608033. papaya  The corner store … active      516. 7.73 
# … with 90 more rows

I like that it shows the type in the column output.

I can fix the wrapping issues by doing:

> print(td, n = 10, width = Inf, n_extra = 0)
# A tibble: 100 × 11
       x     y     z char  long_char                      large_num words     
   <dbl> <dbl> <int> <chr> <chr>                              <dbl> <chr>     
 1  4.17 0.564     1 H     8Ol63ZRclwzzvMUSVWK5sgiE4DhVKI   601011. lemon     
 2  5.39 1.58      2 O     lxIqYtz5whHlZAfpfnDgchVyaILhla   673867. lime      
 3  4.92 2.28      3 A     KBA8X6GtVA3zDE56x80GANVZw2DZ6f   606844. mandarine 
 4  6.21 0.587     4 G     0QGlqx64fX9dC7DTEzT1nZzbODrb1g   479569. eggplant  
 5  6.43 0.668     5 F     fdrg1laPXZT5uxr4lcXFkw1P0gVZxf   551545. loquat    
 6  5.28 2.44      6 J     aqhW8bHx8YdYPT8mWXXNjsFC9WWMOt   402228. pineapple 
 7  4.25 0.316     7 L     u5euDWP0iHDaHqeIdafaHMtxo7Y0sv   587085. blackberry
 8  5.13 0.603     8 V     76yqXz8vtd1C0ew1xPHq6NCiOsyIOO   578124. cucumber  
 9  3.85 2.70      9 B     kxQWSZcveGPulgubUDuhnkro7UV1Zi   548137. strawberry
10  6.04 1.77     10 V     ZbFmoGsqd65FPKHIOb1c2epoMsR2gB   608033. papaya    
   sent                                          more_words    x2    y2
   <chr>                                         <chr>      <dbl> <dbl>
 1 The box was thrown beside the parked truck.   a          3259. 3.46 
 2 He knew the skill of the great young actress. able       8090. 1.74 
 3 Gray paint stretched for miles around.        about       479. 1.33 
 4 The fur of cats goes by many names.           absolute   4270. 8.26 
 5 Pure bred poodles have curls.                 accept      707. 5.76 
 6 A dash of pepper spoils beef stew.            account    5685. 1.61 
 7 Dig deep in the earth for pirate's gold.      achieve    7315. 0.257
 8 The child crawled into the dense grass.       across     1384. 0.613
 9 Sell your gift to a buyer at a good gain.     act        6796. 1.46 
10 The corner store was robbed last night.       active      516. 7.73 
# … with 90 more rows

But I can't figure out how to get tibbles to print the first and last N rows like data.table does.

2 Answers

You can try the tidyext package by Michael Clark. It has a head_tail function which allows showing the head and tail simultaneously.

# libraries ---------------------------------------------------------------
library(dplyr)
library(tidyext)

mtcars %>% 
  head_tail()
#>                   index  mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#> Mazda RX4             1 21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
#> Mazda RX4 Wag         2 21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
#> Datsun 710            3 22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
#> Hornet 4 Drive        4 21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
#> Hornet Sportabout     5 18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
#> Valiant               6 18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1
#> Porsche 914-2        27 26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2
#> Lotus Europa         28 30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2
#> Ford Pantera L       29 15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4
#> Ferrari Dino         30 19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6
#> Maserati Bora        31 15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8
#> Volvo 142E           32 21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2

Created on 2022-08-28 by the reprex package (v2.0.1)

Using tidyverse

library(tidyverse)    
d %>% rbind(head(., 5), tail(., 5))
Related