简体   繁体   English

将漂亮的 data.frames/tables 打印到控制台

[英]Print pretty data.frames/tables to console

Is there a way to print small data.frames to the console in a more readable manner?有没有办法以更易读的方式将小data.frames打印到控制台?

For example, would it be possible to output to the console:例如,是否可以输出到控制台:

library(MASS)   
iris[1:5, ]

  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa

as作为

iris[1:5, ]

  +--------------+-------------+--------------+-------------+---------+
  | Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species |
  +--------------+-------------+--------------+-------------+---------+
1 |          5.1 |         3.5 |          1.4 |         0.2 |  setosa |
2 |          4.9 |         3.0 |          1.4 |         0.2 |  setosa |
3 |          4.7 |         3.2 |          1.3 |         0.2 |  setosa |
4 |          4.6 |         3.1 |          1.5 |         0.2 |  setosa |
5 |          5.0 |         3.6 |          1.4 |         0.2 |  setosa |
  +--------------+-------------+--------------+-------------+---------+

I realise for large data.frames it would take up an unnecessary amount of time, but if it's an option, I would like to be able to look at small frames in a more structured manner.我意识到对于大型data.frames会占用不必要的时间,但如果这是一个选项,我希望能够以更结构化的方式查看小帧。

In particular, when I have two text fields next to each other, it would be much easier with a pipe between the two fields to separate them, as the spacing between words is the same size as the spacing between columns.特别是,当我有两个彼此相邻的文本字段时,在两个字段之间使用管道将它们分开会容易得多,因为单词之间的间距与列之间的间距大小相同。

Thanks谢谢

In case it helps anyone, I just stumbled across the fact that knitr 's kable achieves a nice pretty print.如果它对任何人有帮助,我只是偶然发现knitrkable实现了漂亮漂亮的打印。 Combine with some of the .Rprofile suggestions above, this seems to achieve what I had in mind.结合上面的一些.Rprofile建议,这似乎实现了我的想法。

> knitr::kable(head(iris))

| Sepal.Length| Sepal.Width| Petal.Length| Petal.Width|Species |
|------------:|-----------:|------------:|-----------:|:-------|
|          5.1|         3.5|          1.4|         0.2|setosa  |
|          4.9|         3.0|          1.4|         0.2|setosa  |
|          4.7|         3.2|          1.3|         0.2|setosa  |
|          4.6|         3.1|          1.5|         0.2|setosa  |
|          5.0|         3.6|          1.4|         0.2|setosa  |
|          5.4|         3.9|          1.7|         0.4|setosa  |

I had the same problem recently and came across the huxtable package.我最近遇到了同样的问题,并遇到了huxtable包。 It is very flexible and maybe a litte overkill for just nicer console output, but it served me very well.它非常灵活,对于更好的控制台输出来说可能有点矫枉过正,但它非常适合我。

Here is how you could solve your problem using huxtable :以下是使用huxtable解决问题的方法:

library(huxtable)
library(magrittr)

small_iris <- iris[1:5, ]

iris_hux <- 
  hux(small_iris) %>% 
  add_colnames() %>% 
  set_bold(row = 1, col = everywhere, value = TRUE) %>% 
  set_all_borders(TRUE)

I think all functions speak for themselves.我认为所有功能不言自明。 For a thorough introduction, see https://hughjonesd.github.io/huxtable/huxtable.html#adding-row-and-column-names .有关详细介绍,请参阅https://hughjonesd.github.io/huxtable/huxtable.html#adding-row-and-column-names

print_screen(iris_hux) yield this output (in the console!): print_screen(iris_hux)产生此输出(在控制台中!):

在此处输入图片说明

I have not figured out yet how to suppress the bottom information on the column names.我还没有想出如何抑制列名的底部信息。 So if someone knows, please comment!所以如果有人知道,请评论!

EDIT: In order to suppress the column names at the bottom, use colnames = FALSE inside print_screen() .编辑:为了抑制底部的列名,请在print_screen()内使用colnames = FALSE

There are a couple of methods you could try.您可以尝试几种方法。

  1. Add a couple of helper functions to your .Rprofile .向您的.Rprofile添加几个辅助函数。 In my profile, I have在我的个人资料中,我有

    hh = function(d) if(class(d)=="matrix"|class(d)=="data.frame") d[1:5,1:5]

    This function prints the top left hand corner of the data frame.此函数打印数据框的左上角。 I also have我也有

    ht = function(d, n=6) rbind(head(d, n), tail(d,n))
  2. Create your own S3 print function for data frames, eg为数据框创建您自己的S3打印功能,例如

    print.data.frame = function(x, ..., digits = NULL, quote = FALSE, right = TRUE, row.names = TRUE) message("hi")
  3. Use a package, eg dplyr .使用包,例如dplyr However, this is a bit overkill if all you want is pretty printing.但是,如果您只想要漂亮的打印效果,这有点矫枉过正。

If you are also open to printing your output into the (RStudio) viewer pane rather than the console, I would recommend using the DT package.如果您也愿意将输出打印到 (RStudio) 查看器窗格而不是控制台,我建议您使用DT包。

library(DT)
datatable(iris)

This has several advantages, I think: the output is pretty and well-arranged, the package is able to display large data frames without becoming cumbersome and it is highly customizable to boot.我认为这有几个优点:输出漂亮且排列整齐,包能够显示大数据帧而不会变得笨重,并且可以高度自定义启动。

在此处输入图片说明

tibble s are printed with colour formatting in the console: tibble s 在控制台中以颜色格式打印:

library(tidyverse)

x <- as_tibble(mtcars)
x

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM