Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a global option to adjust the default setting of tibbles that displays only 10 rows?

Tags:

r

tibble

I often use tibbles and am quite satisfied with them. The only recurring issue that bothers me is having to constantly add print(n = ...) every time I want to view more than 10 rows. I'm aware that I could convert them to data frames using as.data.frame(), but I'd prefer to set a global print option, such as print(n=100), for all tibbles until I decide to change it.

So far, what I've been doing is

using print(n=....)

library(dplyr)

mtcars %>% 
  as_tibble() %>% 
  print(n=20)

or using as.data.frame()

mtcars %>% 
  as_tibble() %>% 
  as.data.frame()

or with a custom function (similar to data.table feature)

tar_head_tail <- function(data, nh = 15, nt = 15) {
  library(dplyr)
  library(crayon)
  
  x <- data %>%
    as.data.frame() %>%
    tibble::rownames_to_column("row_number") %>%
    head(n = nh)
  
  y <- data %>%
    as.data.frame() %>%
    tibble::rownames_to_column("row_number") %>%
    tail(n = nt)
  
  head_tail <- bind_rows(x, y) %>%
    tibble::column_to_rownames("row_number")
  
  # Apply color
  cat(green("Head:\n"))
  print(x)
  cat(red("\nTail:\n"))
  print(y)
  
  return(invisible(head_tail))
}

iris %>% 
  tar_head_tail()

How can we set a global print (such as print(n=20)) option for all tibbles until we decide to change it?

Update: What I expect:

When I do:

mtcars %>% 
  as_tibble()

it should show after setting global print(n=20):

# A tibble: 32 × 11
     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
 1  21       6 160     110  3.9   2.62  16.5     0     1     4     4
 2  21       6 160     110  3.9   2.88  17.0     0     1     4     4
 3  22.8     4 108      93  3.85  2.32  18.6     1     1     4     1
 4  21.4     6 258     110  3.08  3.22  19.4     1     0     3     1
 5  18.7     8 360     175  3.15  3.44  17.0     0     0     3     2
 6  18.1     6 225     105  2.76  3.46  20.2     1     0     3     1
 7  14.3     8 360     245  3.21  3.57  15.8     0     0     3     4
 8  24.4     4 147.     62  3.69  3.19  20       1     0     4     2
 9  22.8     4 141.     95  3.92  3.15  22.9     1     0     4     2
10  19.2     6 168.    123  3.92  3.44  18.3     1     0     4     4
11  17.8     6 168.    123  3.92  3.44  18.9     1     0     4     4
12  16.4     8 276.    180  3.07  4.07  17.4     0     0     3     3
13  17.3     8 276.    180  3.07  3.73  17.6     0     0     3     3
14  15.2     8 276.    180  3.07  3.78  18       0     0     3     3
15  10.4     8 472     205  2.93  5.25  18.0     0     0     3     4
16  10.4     8 460     215  3     5.42  17.8     0     0     3     4
17  14.7     8 440     230  3.23  5.34  17.4     0     0     3     4
18  32.4     4  78.7    66  4.08  2.2   19.5     1     1     4     1
19  30.4     4  75.7    52  4.93  1.62  18.5     1     1     4     2
20  33.9     4  71.1    65  4.22  1.84  19.9     1     1     4     1
# ℹ 12 more rows

I want to avoid the additional line of print(n=....), or print() etc...

like image 638
TarJae Avatar asked Oct 29 '25 15:10

TarJae


1 Answers

dplyr and other tidyverse packages import pillar for formatting printed output. The docs list various options to control how tibbles are printed.

For rows:

pillar.print_max: Maximum number of rows printed, default: 20. Set to Inf to always print all rows.

pillar.print_min: Number of rows printed if the table has more than print_max rows, default: 10.

options(pillar.print_min = 100)

mtcars |>
    dplyr::as_tibble()
# prints all 32 rows of mtcars

iris |>
    dplyr::as_tibble()
# prints the first 100 rows of iris
like image 196
SamR Avatar answered Nov 01 '25 04:11

SamR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!