gt icon indicating copy to clipboard operation
gt copied to clipboard

Add option to override `tab_source_note`

Open marianschmidt opened this issue 3 years ago • 1 comments

I find it quite neat that gt allows to override (delete) existing optional table parts like headers by using existing gt objects and re-applying the tab_*() and setting the label to NULL. This comes handy for different journals sometimes requiring to have headers and footers outside the table object. However, this currently only seems to work for tab_header() and not for tab_source_note(). Would be nice to also have the ability to reset existing source notes.

library(gt)

tab_1 <-
  gtcars %>%
  dplyr::select(mfr, model, msrp) %>%
  dplyr::slice(1:5) %>%
  gt() %>%
  tab_header(title = "A simple Table",
             subtitle = "with a subtitle") %>%
  tab_source_note(
    source_note = "From edmunds.com"
  )

overriding headers works, but overriding tab_source_note does not work:

tab_1 %>%
  tab_header(title = NULL,
             subtitle = NULL) %>%
  tab_source_note(source_note = NULL)
mfr model msrp
Ford GT 447000
Ferrari 458 Speciale 291744
Ferrari 458 Spider 263553
Ferrari 458 Italia 233509
Ferrari 488 GTB 245400
From edmunds.com

Created on 2021-06-08 by the reprex package (v2.0.0)

Session info
sessioninfo::session_info()
#> - Session info ---------------------------------------------------------------
#>  setting  value                       
#>  version  R version 4.0.5 (2021-03-31)
#>  os       Windows 10 x64              
#>  system   x86_64, mingw32             
#>  ui       RTerm                       
#>  language (EN)                        
#>  collate  German_Germany.1252         
#>  ctype    German_Germany.1252         
#>  tz       Europe/Berlin               
#>  date     2021-06-08                  
#> 
#> - Packages -------------------------------------------------------------------
#>  package     * version date       lib source        
#>  assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.0.0)
#>  backports     1.2.1   2020-12-09 [1] CRAN (R 4.0.3)
#>  checkmate     2.0.0   2020-02-06 [1] CRAN (R 4.0.2)
#>  cli           2.5.0   2021-04-26 [1] CRAN (R 4.0.5)
#>  colorspace    2.0-1   2021-05-04 [1] CRAN (R 4.0.5)
#>  crayon        1.4.1   2021-02-08 [1] CRAN (R 4.0.3)
#>  DBI           1.1.1   2021-01-15 [1] CRAN (R 4.0.3)
#>  digest        0.6.27  2020-10-24 [1] CRAN (R 4.0.3)
#>  dplyr         1.0.6   2021-05-05 [1] CRAN (R 4.0.5)
#>  ellipsis      0.3.2   2021-04-29 [1] CRAN (R 4.0.5)
#>  evaluate      0.14    2019-05-28 [1] CRAN (R 4.0.0)
#>  fansi         0.5.0   2021-05-25 [1] CRAN (R 4.0.5)
#>  fs            1.5.0   2020-07-31 [1] CRAN (R 4.0.2)
#>  generics      0.1.0   2020-10-31 [1] CRAN (R 4.0.3)
#>  ggplot2       3.3.3   2020-12-30 [1] CRAN (R 4.0.3)
#>  glue          1.4.2   2020-08-27 [1] CRAN (R 4.0.2)
#>  gt          * 0.3.0   2021-05-12 [1] CRAN (R 4.0.5)
#>  gtable        0.3.0   2019-03-25 [1] CRAN (R 4.0.0)
#>  highr         0.9     2021-04-16 [1] CRAN (R 4.0.5)
#>  htmltools     0.5.1.1 2021-01-22 [1] CRAN (R 4.0.3)
#>  knitr         1.33    2021-04-24 [1] CRAN (R 4.0.5)
#>  lifecycle     1.0.0   2021-02-15 [1] CRAN (R 4.0.3)
#>  magrittr      2.0.1   2020-11-17 [1] CRAN (R 4.0.3)
#>  munsell       0.5.0   2018-06-12 [1] CRAN (R 4.0.0)
#>  pillar        1.6.1   2021-05-16 [1] CRAN (R 4.0.5)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.0.0)
#>  purrr         0.3.4   2020-04-17 [1] CRAN (R 4.0.0)
#>  R6            2.5.0   2020-10-28 [1] CRAN (R 4.0.3)
#>  reprex        2.0.0   2021-04-02 [1] CRAN (R 4.0.5)
#>  rlang         0.4.11  2021-04-30 [1] CRAN (R 4.0.5)
#>  rmarkdown     2.8     2021-05-07 [1] CRAN (R 4.0.5)
#>  sass          0.4.0   2021-05-12 [1] CRAN (R 4.0.5)
#>  scales        1.1.1   2020-05-11 [1] CRAN (R 4.0.2)
#>  sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 4.0.0)
#>  stringi       1.6.2   2021-05-17 [1] CRAN (R 4.0.5)
#>  stringr       1.4.0   2019-02-10 [1] CRAN (R 4.0.0)
#>  styler        1.4.1   2021-03-30 [1] CRAN (R 4.0.4)
#>  tibble        3.1.2   2021-05-16 [1] CRAN (R 4.0.5)
#>  tidyselect    1.1.1   2021-04-30 [1] CRAN (R 4.0.5)
#>  utf8          1.2.1   2021-03-12 [1] CRAN (R 4.0.3)
#>  vctrs         0.3.8   2021-04-29 [1] CRAN (R 4.0.5)
#>  withr         2.4.2   2021-04-18 [1] CRAN (R 4.0.5)
#>  xfun          0.23    2021-05-15 [1] CRAN (R 4.0.5)
#>  yaml          2.2.1   2020-02-01 [1] CRAN (R 4.0.0)
#> 
#> [1] C:/Users/ga27jar/Documents/R/win-library/4.0
#> [2] C:/Program Files/R/R-4.0.5/library

marianschmidt avatar Jun 07 '21 22:06 marianschmidt

I think this is a great idea and one that aligns with the direction of the API. A small challenge here is that each call of tab_source_note() creates a new source note without an ID (there is an informal index though). So, to do this we might generally make some changes to enable modification of existing notes.

rich-iannone avatar Jun 12 '21 18:06 rich-iannone