代码之家  ›  专栏  ›  技术社区  ›  TarJae

是否有全局选项来调整仅显示10行的tibbles的默认设置?

  •  3
  • TarJae  · 技术社区  · 2 年前

    我经常使用tibbles,对它们很满意。唯一困扰我的反复出现的问题是必须不断添加 print(n = ...) 每次我想查看10行以上。我知道我可以使用将它们转换为数据帧 as.data.frame() ,但我更愿意设置全局打印选项,例如 print(n=100) ,直到我决定改变它。

    到目前为止,我一直在做的是

    使用 print(n=....)

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

    或使用 作为.data.frame()

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

    或具有自定义功能(类似于data.table功能)

    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()
    

    在我们决定更改之前,如何为所有tibble设置全局打印(如打印(n=20))选项?

    更新:我的期望:

    当我这样做时:

    mtcars %>% 
      as_tibble()
    

    它应该在设置全局打印(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
    

    我想避开的附加线路 打印(n=….) print()

    1 回复  |  直到 2 年前
        1
  •  3
  •   SamR    2 年前

    dplyr 和其他 tidyverse 程序包导入 pillar 用于格式化打印输出。文档列出了各种 options 以控制tibbles的打印方式。

    对于行:

    pillar.print_max :打印的最大行数,默认值: 20 。设置为 Inf 始终打印所有行。

    pillar.print_min :如果表的行数超过 print_max 行,默认值: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