Search code examples
rflextablegtsummary

How do I rearrange columns in gtsummary or flextable?


In reference to this answer [confidence intervals] (https://stackoverflow.com/a/66891473/13734451), how can rearrange the columns in gtsummary or flextable? There is a way to do this using gt package (as below) but that wont knit to Ms Word. Any leads?

final_tbl %>% as_gt() %>%
  cols_move(
    columns = vars(Male_ci),
    after = vars(stat_1)
  )

Solution

  • Here's an example of moving a column of a gtsummary object. This script moves the p.value column to after the label column

    library(gtsummary)
    packageVersion("gtsummary")
    #> '1.4.0'
    
    # build table
    tbl <-
      trial %>%
      select(age, trt) %>%
      tbl_summary(by = trt, missing = "no") %>%
      add_p() 
    
    # move p.value column
    tbl %>%
      modify_table_body(~.x %>% dplyr::relocate(p.value, .after = label))
    

    enter image description here