rstudio / gt

Easily generate information-rich, publication-quality tables from R
https://gt.rstudio.com
Other
2.04k stars 210 forks source link

Spanner row #918

Open SteveBrouwer opened 2 years ago

SteveBrouwer commented 2 years ago

Hi,

I'm really enjoying GT Please see my table attached, and my code below.

I would like to merge rows in the first column. eg both bigeye rows in first column to be a single cell same for yellowfin and skipjack. This seems to be unnecessarily hard for something relatively common, so a spanner column is available but not row? I do not want to do this as a row group label.

Two other minor questions 1) How do I continue my line to the right of column 1 into the column label row? 2) in my subscripting function (fn) line 25 any idea why my "None" is coming out as a NA?

Lastly in Hmisc there is a longtable function that allows a table to carry on over multiple pages, is there something similar in GT without doing it manually?

tab_rfmo_man_stds

######################################################################## msy<-data.frame(Stock=c("Bigeye", "Bigeye","Skipjack","Skipjack", "Yellowfin", "Yellowfin"), Reference_pt=c(rep(c("TRP/Objective", "LRP"),3)), WCPFC=c("1.65SBMSY", "SBMSY", "2.14SBMSY", "SBMSY","2.85SBMSY", "SBMSY"), IOTC=c("SBMSY", "0.5SBMSY", "1.8SBMSY", "0.9SBMSY","SBMSY", "0.4SBMSY"), ICCAT=c("SBMSY", "", "SBMSY", "","SBMSY", ""), IATCC=c("SBMSY", "0.5SBMSY", "SBMSY", "0.5SBMSY","SBMSY", "0.5SBMSY"))

msy_tab_1<-msy%>% gt() %>% tab_header( title = md("Tuna RFMO management standards") ) %>% tab_source_note(md("ISSF. 2021. Status of the world fisheries for tuna. Sept. 2021. ISSF Technical Report 2021-13. International Seafood Sustainability Foundation, Washington, D.C., USA."))

to get subscripting

library(stringr)

msy_tab_1<-msy_tab_1%>% text_transform( locations = cells_body( columns = c(WCPFC,IOTC, ICCAT, IATCC) ), fn = function(x){ msya <- str_extract(x, "MSY") sb <- str_extract(x, "SB") dec<-str_extract(x, "\d+\.*\d") dec<-ifelse(is.na(dec), " " , dec) tms<-ifelse(dec == " ", "" , " x ") ifelse(sb == "SB", glue::glue("{dec}{tms}{sb}{msya}"), "None") } ) ################################################################# msy_tab_1a<-msy_tab_1%>% tab_options( column_labels.background.color = "cyan", heading.background.color = "grey70") %>%

Apply different style to the title

tab_style( locations = cells_title(groups = c("title")), style = list( cell_text(weight = "bold", size = 50, color = "black") )) %>% tab_style( style = list( cell_borders( sides = "right", color = "grey70", weight = px(2) ) ), locations = list( cells_body( columns = c('Stock')) ))

msy_tab_1a

Apply different style to the column headers

msy_tab_1b<-msy_tab_1a %>% tab_style( style = cell_borders( sides = c("top", "bottom"), color = "grey80", weight = px(1.5), style = "solid" ), locations = cells_body( columns = everything(), rows = everything() ) )

add column color

msy_tab_1c<-msy_tab_1b %>% tab_style( style = list( cell_fill(color = "cyan") ), locations = cells_body( columns = c("Stock")))

msy_tab_1d<-msy_tab_1c %>%

Apply different style to the column headers

tab_style( locations = cells_column_labels(), style = list( cell_text(weight = "bold", color = "black") ))

Change the column width

msy_tab_1e<-msy_tab_1d %>% cols_width( Stock ~ px(110), Reference_pt ~ (120), WCPFC ~ px(100), IOTC ~ px(100), ICCAT ~ px(90), IATCC ~ px(100) )

msy_tab_1e %>% gtsave( "tabs/tab_rfmo_man_stds.png", expand = 10) ########################################################################

jonfoong commented 1 year ago

I would second this. I came here looking to open a similar issue but see that this is still open. Not sure where this ranks in list of priorities, but im currently trying to display all conditional interaction effects of high dimensional interaction term and being able to span on row would be really helpful. I don't know any other package that does this so am holding out hope for gt to roll out this function soon.

obsaditelnost commented 2 months ago

It's already possible to achieve this with groupname_col:

msy <- data.frame(
  Stock = c("Bigeye", "Bigeye", "Skipjack", "Skipjack", "Yellowfin", "Yellowfin"),
  Reference_pt = c(rep(c("TRP/Objective", "LRP"), 3)),
  WCPFC = c("1.65SBMSY", "SBMSY", "2.14SBMSY", "SBMSY", "2.85SBMSY", "SBMSY"),
  IOTC = c("SBMSY", "0.5SBMSY", "1.8SBMSY", "0.9SBMSY", "SBMSY", "0.4SBMSY"),
  ICCAT = c("SBMSY", "", "SBMSY", "", "SBMSY", ""),
  IATCC = c("SBMSY", "0.5SBMSY", "SBMSY", "0.5SBMSY", "SBMSY", "0.5SBMSY")
)

msy_tab_1 <- msy %>%
  gt(groupname_col = "Stock") |>
  tab_header(
    title = md("Tuna RFMO management standards")
  ) |>
  tab_source_note(md("ISSF. 2021. Status of the world fisheries for tuna. Sept. 2021. ISSF
                      Technical Report 2021-13. International Seafood Sustainability Foundation,
                      Washington, D.C., USA.")) |>
  tab_options(row_group.as_column = TRUE) |>
  tab_stubhead("Stock")

msy_tab_1

image