Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Follow-up check in on #417 (more granular control over printing tibbles) #1016

Open
jwhendy opened this issue Jan 13, 2022 · 2 comments
Open

Comments

@jwhendy
Copy link

jwhendy commented Jan 13, 2022

I love dplyr and use it from emacs/org-mode for including output in my results. I was looking into how to simply disable the text #A tibble: n x m from every table. A meandering path led me through these various relevant bits of info:

Since #417 is locked, I wanted to follow up as we are at ~4yrs since. For my input, my hope was to achieve simply turning off this persistent label of #A tibble: n x m. I'm okay with keeping variable types, and see there's already a max_footer_lines option, which I think would still give me convenient control over including the total size of the tibble when it doesn't fit in the screen.

At present, many of my tibbles are small and fit within the width of my document, which goes to "normal humans" who I don't want to distract with this constant labeling of unnecessary information. I still like the footer where the output doesn't fit, as it's nice to know "Oh, I'm not seeing everything because this is actually 300k lines long with 50 columns."

I'm not actually sure where this inserted header comes from, assuming I had the R chops to attempt a PR to add e.g. an omit_header = T option. I looked in both tibble and pillar.

[pillar] $ grep -R "A tibble" *
man/tbl_format_body.Rd:\item{x}{A tibble-like object.}
man/tbl_format_footer.Rd:\item{x}{A tibble-like object.}
man/tbl_format_header.Rd:\item{x}{A tibble-like object.}
R/tbl-format-body.R:#' @param x A tibble-like object.
### the rest are tests or vignettes

[tibble] $ grep -R "A tibble" *
man/new_tibble.Rd:\item{x}{A tibble-like object.}
man/subsetting.Rd:\item{x}{A tibble.}
man/tibble.Rd:A tibble, which is a colloquial term for an object of class
R/new.R:#' @param x A tibble-like object.
R/subsetting.R:#' @param x A tibble.
R/tbl_sum.R:  c("A tibble" = dim_desc(x))
R/tibble.R:#' @return A tibble, which is a colloquial term for an object of class
### the rest are Readme, tests, or vignettes

Thanks for any updates on the likelihood of these more granular options as referenced in the article above, as well as for thoughts on the ease with which just that header could be removed (and doing so made to be an option as part of the print options).

P.S. Sorry if this should be in pillar... I'm putting it here because #417 is essentially the same and lives here. If you want, I'll copy/paste, link here, and close this.

@jwhendy
Copy link
Author

jwhendy commented Jan 13, 2022

Ok, digging in, it does appear to be tibble/R/tbl_sum.R that contributes the header:

#' @export
tbl_sum.tbl_df <- function(x, ...) {
  c("A tibble" = dim_desc(x))
}

After simply commenting out the single line (yielding an empty function, I suppose), the behavior seems to do what I want (with no clue on side effects):

> library(tibble)
> foo <- tibble( 
  x = c(1, 2, 3),
  y = c("a", "b", "c"))
foo

      x y    
  <dbl> <chr>
1     1 a    
2     2 b    
3     3 c    

Could someone with more knowledge chime in on a) if there are likely to be other widespread consequences to this and b) thoughts on toggling this inclusion via an additional option like desc_header = F (perhaps there would be another like types_header if someone also didn't want the <dbl> <chr> bits, for example).

@krlmlr
Copy link
Member

krlmlr commented Feb 22, 2023

Thanks for raising this, and thank you for your patience. This fell through the cracks.

Custom printing can be achieved by overriding methods. Tibbles are of class c("tbl_df", "tbl", "data.frame"), and the "tbl" class is responsible for printing. We can't overwrite the existing .tbl methods provided by the pillar package, but we can provide our own methods for the "tbl_df" subclass. The safest (but also most cumbersome) way is to implement your own tibble subclass. See https://pillar.r-lib.org/articles/extending.html#header for a description of the customization of the header.

There's a new article in https://tibble.tidyverse.org/dev/articles/extending.html (source: #1512) that has more details. I wonder what other documentation could be useful.

Given the good customization options, I'd rather avoid adding new options that control the display.

library(tibble)

tbl_format_header.tbl <- function(x, setup, ...) {
}

# Still shows header
tibble(a = 1)
#> # A tibble: 1 × 1
#>       a
#>   <dbl>
#> 1     1

tbl_format_header.tbl_df <- function(x, setup, ...) {
}

# Hides header
tibble(a = 1)
#>       a
#>   <dbl>
#> 1     1

# Safest option: custom subclass
my_tibble <- function(...) {
  new_tibble(tibble(...), class = c("my_tbl_df", "tbl_df", "tbl"))
}

tbl_format_header.my_tbl_df <- function(x, setup, ...) {
}

my_tibble(a = 1)
#>       a
#>   <dbl>
#> 1     1

Created on 2023-02-22 with reprex v2.0.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants