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.
message()statement right beforeas_tibbleand you're done ... ?knitr-specific tweak.knitroverrides R's "useprintto display an object" in chunks. It usesknit_printby default andnormal_printto 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 withknitr, overrideknit_print.data.frameif that suits you better.options(max.print = 6L)will suffice.options(max.print=6L)will restrict to 6 "cells" of a frame ... try that withmtcarsand I see 0 rows; incrementing it, I see one row whenmax.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.Grothendieckmaxis 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 useuntrace(print.data.frame)or reissue thetracewith a different value.