1

I'm working with a variety of data frames across multiple projects. For better readability and a concise overview in the console, I prefer the tibble format, which shows only the first few rows along with other helpful information. However, many of the data frames I load are standard data frames, so I need to manually convert them using as_tibble() before printing.

This manual conversion becomes cumbersome as the number of data frames grows. Is there a way to globally override R’s default printing method or set an option (using options()) so that every data frame, regardless of its primary class, is printed in tibble format?

For instance, I considered overriding the default print.data.frame method like this:

library(tibble) # Save the original print.data.frame function old_print_df <- base::print.data.frame # Override print.data.frame to convert data frames to tibbles before printing print.data.frame <- function(x, ...) { if (!inherits(x, "tbl_df")) { x <- as_tibble(x) } print(x, ...) } 

This works, but I would also like to display an informational message (or warning) whenever a conversion occurs, so I know a data frame was printed as a tibble. Or add the information "revert back to original: print.data.frame <- old_print_df"

Is there a more centralized or elegant approach to accomplish this - either through an option in R or by refining this override - so that every data frame gets printed as a tibble with an accompanying message? Any suggestions or best practices would be appreciated.

8
  • 6
    This seems fine. Add a message() statement right before as_tibble and you're done ... ? Commented Mar 29 at 2:21
  • 6
    I think both you and @BenBolker have got the correct approach. I'd add just a little knitr-specific tweak. knitr overrides R's "use print to display an object" in chunks. It uses knit_print by default and normal_print to revert to default to usual behaviour. I don't know how it does this. But you could investigate to see how Yihui has done it. Or, if you're working with knitr, override knit_print.data.frame if that suits you better. Commented Mar 29 at 8:57
  • 2
    If it is sufficient to just print the first 6, say, rows, and forget about the message and the ending rows then options(max.print = 6L) will suffice. Commented Mar 29 at 17:31
  • 2
    In my testing, options(max.print=6L) will restrict to 6 "cells" of a frame ... try that with mtcars and I see 0 rows; incrementing it, I see one row when max.print=11L, which corresponds to the number of columns in the frame. I don't know of a base option to limit the number of rows printed in absence of knowing the number of columns as well. @G.Grothendieck Commented Mar 29 at 18:15
  • 3
    @r2evans, You are right. It specifies entries, not rows. How annoying. Anyways try this instead. If max is not specified then it will print 6 rows. Change 6 as needed. trace(print.data.frame, quote(max <- if (is.null(max)) max <- 6 * ncol(x)), print = FALSE) . To undo it use untrace(print.data.frame) or reissue the trace with a different value. Commented Mar 29 at 20:29

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.