1 year ago

#359034

test-img

Jesus Esteban

tab_row_names does not accept helpers if gt table comes from gtsummary::tbl_summary

According to help pages https://rdrr.io/cran/gt/man/tab_row_group.html , tab_row_names accept helpers to locate rows. Actually, it works with a gt table, but it does not work when the gt table comes from the transformation of a tbl_summary using as_gt(). Example with a gt table working as expected.

 gtcars  %>% 
    dplyr::select(model, year, hp, trq)  %>% 
    dplyr::slice(1:8)  %>% 
    gt(rowname_col = "model")  %>% 
    tab_row_group(
      label = "GT_CARS",
      rows = contains("GT")
    ) %>% 
    tab_row_group(
      label = "400",
      rows = starts_with("4")
    )

Example using as_gt() after tbl_summary with no result.

gtcars %>% 
      gtsummary::tbl_summary(by=year) %>% 
      as_gt() %>% 
      tab_row_group(
        label = "m is in the variable name",
        rows = contains("m")
      ) 

It works if we use indexes.

mtcars %>% 
  gtsummary::tbl_summary(by=gear) %>% 
  as_gt() %>% 
  tab_row_group(label='m is in the variable name with indexes',
                rows=c(1,12))

I guess this is probably due to the fact that tab_row_group checks the condition in gtobject$_data while in the gtobject generated after tbl_summary, variable names are in gtobject$_data$variable.

So far I have overcome this problem by using a function to retrieve indexes within the getobj$data$ variable when the condition is met, but this implies saving the object before.

  rtrv_i<-function(vector,txt){
    which(vector%in%vector[stringr::str_detect(string=vector,pattern = txt)])
  }
  #I am sure there must be an appropriate function for this but I haven´t found it yet.

Is there any way to use helpers for selecting rows for tab_row_groups in gt tables coming after the as_gt() transformation?

Thank you.

helper

gtsummary

gt

0 Answers

Your Answer

Accepted video resources