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

Adding a footnote to multiple spanner columns #228

Closed
gadenbuie opened this issue Mar 20, 2019 · 2 comments · Fixed by #355
Closed

Adding a footnote to multiple spanner columns #228

gadenbuie opened this issue Mar 20, 2019 · 2 comments · Fixed by #355

Comments

@gadenbuie
Copy link
Member

I’m trying to attach the same footnote to two column spanner labels and I discovered that the groups argument in cells_column_labels() seems to only take a single group. I’m not sure if this is intended behavior – the documentation doesn’t specify a limitation and the argument is plural rather than singular.

library(tidyverse)
library(gt)

Creating some simple example data with two spanner columns.

example_data <-
  tibble(
    letter = rep(c("A", "B"), each = 3),
    id = rep(1:3, 2),
    x = runif(6),
    y = runif(6)
  ) %>% 
  gather(var, value, x:y) %>% 
  unite(column, letter, var) %>% 
  spread(column, value)

example_data
#> # A tibble: 3 x 5
#>      id   A_x   A_y   B_x    B_y
#>   <int> <dbl> <dbl> <dbl>  <dbl>
#> 1     1 0.456 0.789 0.492 0.564 
#> 2     2 0.862 0.774 0.733 0.895 
#> 3     3 0.278 0.926 0.718 0.0807

Referencing the spanner columns as c("A", "B") doesn’t work.

ex_gt <-
  example_data %>% 
  gt() %>% 
  cols_split_delim("_", everything())
ex_gt %>% 
  tab_footnote(
    footnote = "Source: the definitive guide to the alphabet",
    location = cells_column_labels(groups = c("A", "B"))
  )

id

A

B

x

y

x

y

1

0.4559288

0.7890988

0.4918227

0.56427618

2

0.8617048

0.7735747

0.7332311

0.89511556

3

0.2775823

0.9261118

0.7178421

0.08071516

It also doesn’t work if the spanner column label is wrapped in c().

ex_gt %>% 
  tab_footnote(
    footnote = "Source: the definitive guide to the alphabet",
    location = cells_column_labels(groups = c("A"))
  )

id

A

B

x

y

x

y

1

0.4559288

0.7890988

0.4918227

0.56427618

2

0.8617048

0.7735747

0.7332311

0.89511556

3

0.2775823

0.9261118

0.7178421

0.08071516

But adding the footnote to a single column spanner group works.

ex_gt %>% 
  tab_footnote(
    footnote = "Source: the definitive guide to the alphabet",
    location = cells_column_labels(groups = "A")
  ) %>% 
  tab_footnote(
    footnote = "Source: the definitive guide to the alphabet",
    location = cells_column_labels(groups = "B")
  )

id

A1

B1

x

y

x

y

1

0.4559288

0.7890988

0.4918227

0.56427618

2

0.8617048

0.7735747

0.7332311

0.89511556

3

0.2775823

0.9261118

0.7178421

0.08071516

1 Source: the definitive guide to the alphabet

I expected groups to operate in the same way as the columns argument of
cells_column_labels().

ex_gt %>% 
  tab_footnote(
    footnote = "Source: randomness",
    location = cells_column_labels(columns = c("A_x", "B_x"))
  )

id

A

B

x1

y

x1

y

1

0.4559288

0.7890988

0.4918227

0.56427618

2

0.8617048

0.7735747

0.7332311

0.89511556

3

0.2775823

0.9261118

0.7178421

0.08071516

1 Source: randomness

Created on 2019-03-20 by the reprex package (v0.2.1)

Session info
devtools::session_info()
#> ─ Session info ──────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 3.5.3 (2019-03-11)
#>  os       macOS Mojave 10.14.2        
#>  system   x86_64, darwin15.6.0        
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       America/New_York            
#>  date     2019-03-20                  
#> 
#> ─ Packages ──────────────────────────────────────────────────────────────
#>  package     * version    date       lib source                        
#>  assertthat    0.2.0      2017-04-11 [1] CRAN (R 3.5.0)                
#>  backports     1.1.3      2018-12-14 [1] CRAN (R 3.5.0)                
#>  broom         0.5.0      2018-07-17 [1] CRAN (R 3.5.0)                
#>  callr         3.1.1      2018-12-21 [1] CRAN (R 3.5.0)                
#>  cellranger    1.1.0      2016-07-27 [1] CRAN (R 3.5.0)                
#>  checkmate     1.8.5      2017-10-24 [1] CRAN (R 3.5.0)                
#>  cli           1.0.1      2018-09-25 [1] CRAN (R 3.5.0)                
#>  colorspace    1.4-0      2019-01-13 [1] CRAN (R 3.5.2)                
#>  commonmark    1.7        2018-12-01 [1] standard (@1.7)               
#>  crayon        1.3.4      2017-09-16 [1] CRAN (R 3.5.0)                
#>  desc          1.2.0      2018-05-01 [1] CRAN (R 3.5.0)                
#>  devtools      2.0.1      2018-10-26 [1] CRAN (R 3.5.1)                
#>  digest        0.6.18     2018-10-10 [1] CRAN (R 3.5.0)                
#>  dplyr       * 0.8.0.1    2019-02-15 [1] CRAN (R 3.5.2)                
#>  evaluate      0.13       2019-02-12 [1] standard (@0.13)              
#>  fansi         0.4.0      2018-10-05 [1] CRAN (R 3.5.0)                
#>  forcats     * 0.4.0      2019-02-17 [1] CRAN (R 3.5.2)                
#>  fs            1.2.6      2018-08-23 [1] CRAN (R 3.5.0)                
#>  ggplot2     * 3.1.0      2018-10-25 [1] CRAN (R 3.5.0)                
#>  glue          1.3.1      2019-03-12 [1] standard (@1.3.1)             
#>  gt          * 0.1.0      2019-03-20 [1] Github (rstudio/gt@51a812b)   
#>  gtable        0.2.0      2016-02-26 [1] CRAN (R 3.5.0)                
#>  haven         2.0.0      2018-11-22 [1] CRAN (R 3.5.0)                
#>  highr         0.7        2018-06-09 [1] CRAN (R 3.5.0)                
#>  hms           0.4.2      2018-03-10 [1] CRAN (R 3.5.0)                
#>  htmltools     0.3.6      2017-04-28 [1] CRAN (R 3.5.0)                
#>  httr          1.4.0      2018-12-11 [1] CRAN (R 3.5.1)                
#>  jsonlite      1.6        2018-12-07 [1] CRAN (R 3.5.0)                
#>  knitr         1.22       2019-03-08 [1] standard (@1.22)              
#>  lattice       0.20-38    2018-11-04 [1] CRAN (R 3.5.3)                
#>  lazyeval      0.2.1      2017-10-29 [1] CRAN (R 3.5.0)                
#>  lubridate     1.7.4      2018-04-11 [1] CRAN (R 3.5.0)                
#>  magrittr      1.5        2014-11-22 [1] CRAN (R 3.5.0)                
#>  memoise       1.1.0      2017-04-21 [1] CRAN (R 3.5.0)                
#>  modelr        0.1.2      2018-05-11 [1] CRAN (R 3.5.0)                
#>  munsell       0.5.0      2018-06-12 [1] CRAN (R 3.5.0)                
#>  nlme          3.1-137    2018-04-07 [1] CRAN (R 3.5.3)                
#>  pillar        1.3.1      2018-12-15 [1] CRAN (R 3.5.0)                
#>  pkgbuild      1.0.2      2018-10-16 [1] CRAN (R 3.5.0)                
#>  pkgconfig     2.0.2      2018-08-16 [1] CRAN (R 3.5.0)                
#>  pkgload       1.0.2      2018-10-29 [1] CRAN (R 3.5.0)                
#>  plyr          1.8.4      2016-06-08 [1] CRAN (R 3.5.0)                
#>  prettyunits   1.0.2      2015-07-13 [1] CRAN (R 3.5.0)                
#>  processx      3.2.1      2018-12-05 [1] CRAN (R 3.5.0)                
#>  ps            1.3.0      2018-12-21 [1] CRAN (R 3.5.0)                
#>  purrr       * 0.3.1      2019-03-03 [1] CRAN (R 3.5.2)                
#>  R6            2.4.0      2019-02-14 [1] CRAN (R 3.5.2)                
#>  Rcpp          1.0.0      2018-11-07 [1] CRAN (R 3.5.0)                
#>  readr       * 1.3.1      2018-12-21 [1] CRAN (R 3.5.0)                
#>  readxl        1.1.0      2018-04-20 [1] CRAN (R 3.5.0)                
#>  remotes       2.0.2      2018-10-30 [1] CRAN (R 3.5.0)                
#>  rlang         0.3.1.9000 2019-02-03 [1] Github (r-lib/rlang@7243c6d)  
#>  rmarkdown     1.11       2018-12-08 [1] CRAN (R 3.5.0)                
#>  rprojroot     1.3-2      2018-01-03 [1] CRAN (R 3.5.0)                
#>  rvest         0.3.2      2016-06-17 [1] CRAN (R 3.5.0)                
#>  sass          0.1.0.9000 2019-03-20 [1] Github (rstudio/sass@d90395e) 
#>  scales        1.0.0      2018-08-09 [1] CRAN (R 3.5.0)                
#>  sessioninfo   1.1.1      2018-11-05 [1] CRAN (R 3.5.0)                
#>  stringi       1.4.3      2019-03-12 [1] standard (@1.4.3)             
#>  stringr     * 1.4.0      2019-02-10 [1] CRAN (R 3.5.2)                
#>  testthat      2.0.0      2017-12-13 [1] CRAN (R 3.5.0)                
#>  tibble      * 2.0.1      2019-01-12 [1] CRAN (R 3.5.2)                
#>  tidyr       * 0.8.3      2019-03-01 [1] CRAN (R 3.5.2)                
#>  tidyselect    0.2.5      2018-10-11 [1] CRAN (R 3.5.0)                
#>  tidyverse   * 1.2.1      2017-11-14 [1] CRAN (R 3.5.0)                
#>  usethis       1.4.0.9000 2019-02-20 [1] Github (r-lib/usethis@0adfb5d)
#>  utf8          1.1.4      2018-05-24 [1] CRAN (R 3.5.0)                
#>  withr         2.1.2      2018-03-15 [1] CRAN (R 3.5.0)                
#>  xfun          0.5        2019-02-20 [1] standard (@0.5)               
#>  xml2          1.2.0      2018-01-24 [1] CRAN (R 3.5.0)                
#>  yaml          2.2.0      2018-07-25 [1] CRAN (R 3.5.0)                
#> 
#> [1] /Library/Frameworks/R.framework/Versions/3.5/Resources/library
@rich-iannone
Copy link
Member

Thanks for illustrating the problem so clearly with the examples. The groups argument in cells_column_labels() should ideally be able to take multiple groups, so, this needs a fix.

@rich-iannone
Copy link
Member

This is all good now:

tibble(
  letter = rep(c("A", "B"), each = 3),
  id = rep(1:3, 2),
  x = runif(6),
  y = runif(6)
) %>% 
  gather(var, value, x:y) %>% 
  unite(column, letter, var) %>% 
  spread(column, value) %>%
  gt() %>%
  cols_split_delim("_", everything()) %>%
  tab_footnote(
    footnote = "Source: the definitive guide to the alphabet",
    location = cells_column_labels(groups = c("A", "B"))
  )

However, vars(A, B) doesn't work (Error: Column grpname can't be converted from character to quosures), and the select helpers like everything(), starts_with(...) don't work (Error: No tidyselect variables were registered).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment