Print pretty data.frames/tables to console

25,218

Solution 1

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  |

Solution 2

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!):

enter image description here

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

Solution 3

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.

Solution 4

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.

enter image description here

Solution 5

tibbles are printed with colour formatting in the console:

library(tidyverse)

x <- as_tibble(mtcars)
x

enter image description here

Share:
25,218

Related videos on Youtube

Akhil Nair
Author by

Akhil Nair

Updated on March 06, 2020

Comments

  • Akhil Nair
    Akhil Nair over 4 years

    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:

    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.

    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

    • Admin
      Admin almost 9 years
      How about View(iris[1:5,])?
    • misspelled
      misspelled almost 9 years
      Have you tried dplyr package? It has tbl_df function to convert to what Hadley Wickham calls a local data.frame. It is supposed to (and does, IMO) provide more structured and readable output when printing to console. It does not provide separators between fields though.
    • Akhil Nair
      Akhil Nair almost 9 years
      Yeah, I've used both of them. And I'd like it to print the console because I need to interactively change some values between two tables which involves some interpretation of text for ~250 values, so I'm just trying to make it a bit easier to manage. I'm also using data.table for the most part - the output of which i find preferable to dplyr's.
  • Akhil Nair
    Akhil Nair almost 9 years
    All I do want is pretty printing. The objects are data.tables anyway, so are default printed as 5 head, 5 tail rows. Thanks for the pretty keyword though. Seems like a more appropriate title.
  • csgillespie
    csgillespie almost 9 years
    If you want to pretty print, then create function called print.data.table. When a data table is printed to the screen, that function will be executed.
  • Shantanu
    Shantanu about 7 years
    As of knitr 1.16, this is no longer the case (i.e. it no longer pretty prints). @yihui - any ideas?
  • G5W
    G5W almost 7 years
    @Shantanu Works for me with knitr_1.17
  • Shantanu
    Shantanu almost 7 years
    @G5W Thanks for reporting. I realized that the issue is something related to Rmd chunks. In RStudio, create a new Rmd file, execute a chunk (any chunk), then run knitr::kable(head(iris)). The output now doesn't look as clean (e.g. it include attr(,"format") attr(,"class") attr(,"knit_cacheable") etc.)
  • Admin
    Admin about 6 years
    Note that in general you shouldn't need to use print_screen: just evaluating iris_hux would do the same thing.
  • cs0815
    cs0815 over 5 years
    is there a way to suppress scientific notation? This: options(scipen = 999) does not work
  • tjebo
    tjebo about 4 years
    you can now change the print format with the format argument. E.g., the above table would be printed with knitr::kable(head(iris), format = "markdown")