Print pretty data.frames/tables to console

前端 未结 5 990

Is there a way to print small data.frames to the console in a more readable manner?

For example, would it be possible to output to the console:

相关标签:
5条回答
  • 2020-12-03 01:37

    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.

    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.

    0 讨论(0)
  • 2020-12-03 01:39

    There are a couple of methods you could try.

    1. Add a couple of helper functions to your .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, e.g.

      print.data.frame = function(x, ..., digits = NULL, 
                              quote = FALSE, right = TRUE, 
                              row.names = TRUE) 
                          message("hi")
      
    3. Use a package, e.g. dplyr. However, this is a bit overkill if all you want is pretty printing.

    0 讨论(0)
  • 2020-12-03 01:44

    tibbles are printed with colour formatting in the console:

    library(tidyverse)
    
    x <- as_tibble(mtcars)
    x
    

    0 讨论(0)
  • 2020-12-03 01:55

    In case it helps anyone, I just stumbled across the fact that knitr's kable achieves a nice pretty print. Combine with some of the .Rprofile suggestions above, this seems to achieve what I had in mind.

    > 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  |
    
    0 讨论(0)
  • 2020-12-03 02:01

    I had the same problem recently and came across the huxtable package. 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:

    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.

    print_screen(iris_hux) yield this output (in the console!):

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

    0 讨论(0)
提交回复
热议问题