我经常使用碎石,我对它们很满意.唯一困扰我的反复出现的问题是,每次我想查看超过10行时,都必须不断地添加print(n = ...).我知道我可以使用as.data.frame()将它们转换为数据帧,但我更喜欢为所有的小块设置一个全局打印选项,如print(n=100),直到我决定更改它.

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

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

我们如何为所有的tibles设置一个全局打印(比如print(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

我想避免额外的print(n=....)print()等行...

推荐答案

dplyr和其他tidyverse包导入pillar用于格式化打印输出.文档列出了各种options来控制tibles如何打印.

对于行:

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

R相关问答推荐

有没有方法将琴弦完全捕捉到R中的多边形?

从API中抓取R数据SON

在边界外添加注释或标题

手动打印线型gplot

我不能在docker中加载sf

Rplotly中的Sankey Diagram:意外连接&

为什么舍入POSIXct会更改能力以匹配等效的POSIXct?

Ggplot2中的重复注记

如何从像glm这样的模型中提取系数表的相关性?

使用data.table::fcase()而不是dplyr::case_When()时保持值

根据现有列的名称和字符串的存在进行变异以创建多个新列

将二进制数据库转换为频率表

如何指定我的函数应该查找哪个引用表?

提高圣彼得堡模拟的速度

如何为混合模型输出绘制不同的线型?

如何构建一个for循环来循环处理动物ID?

在REST API中使用参数R

组合名称具有模式的列表的元素

如果缺少时间,如何向日期-时间列添加时间

R:部分修改矩阵对角线的有效方法