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.
print.tbl
, so if you want it natively, you'll need to submit a feature-request and justify it (and wait for the CRAN release). – r2evansmytibble %>% ht
– Reilsteinprint.tbl
, see stackoverflow.com/a/68739226/13513328 – Waldi