Customising table width in R Markdown report

I am creating a summary table (Race by symptom status) for an R Markdown report in a Word document output. I am having issues adjusting the width of the table as the table is too wide in the Word document.

We tried changing arguments (fig.width and out.width) in the code chunk however that did not work.

Here is the reproducible summary of the data and table I am trying to create.

Thank you for your help

pacman::p_load(
  datapasta,
  gtsummary,
  reprex)

sampledata <- data.frame(
  stringsAsFactors = FALSE,
              race = c("Black","White","Black",NA,
                       "Other","Other","White","White","White","Other",
                       "White","White","White","White","White","Black",
                       "Black","White","Black","Black","Black","Black","Black",
                       "White",NA,NA,"White","Black","Black","Black",
                       "Black","White","Black","White","White","Black","Other",
                       "White","Other","White","Black","White","White",
                       "White","Asian","White","Black","White","Black",
                       "White"),
      sym_resolved = c(NA,"Unknown symptom status",
                       NA,NA,NA,NA,"No, still symptomatic",NA,NA,NA,NA,
                       "No, still symptomatic",NA,NA,NA,
                       "Yes, date specified below","Yes, date specified below","Yes, date unknown",
                       "Yes, date specified below","Yes, date specified below",
                       "Yes, date specified below",NA,NA,NA,NA,NA,
                       "Yes, date unknown","Yes, date specified below",
                       "Yes, date specified below","Yes, date specified below",NA,
                       "Yes, date specified below","No, still symptomatic",
                       "No, still symptomatic","Yes, date specified below",NA,
                       "No, still symptomatic","No, still symptomatic",
                       "No, still symptomatic","Yes, date unknown","No, still symptomatic",
                       "Yes, date specified below","Yes, date specified below",NA,
                       "No, still symptomatic","No, still symptomatic",NA,
                       "No, still symptomatic","Yes, date specified below",
                       "Yes, date specified below")
)
# dataexample <- linelist_clean %>% 
#     mutate(race = na_if(race, "Unknown")) %>%   # convert "Unknown" to NA in race column
#     mutate(race = str_to_title(race)) %>% # Change values in race column title case  
#      select(race, sym_resolved) %>%   # select columns of interest
#   head(50)
  
# dpasta(dataexample)
  
tbl_summary(sampledata, by = sym_resolved, percent = "row")   # show % in brackets
#> 21 observations missing `sym_resolved` have been removed. To include these observations, use `forcats::fct_na_value_to_level()` on `sym_resolved` column before passing to `tbl_summary()`.
Characteristic No, still symptomatic, N = 111 Unknown symptom status, N = 11 Yes, date specified below, N = 141 Yes, date unknown, N = 31
race
Asian 1 (100%) 0 (0%) 0 (0%) 0 (0%)
Black 2 (18%) 0 (0%) 9 (82%) 0 (0%)
Other 2 (100%) 0 (0%) 0 (0%) 0 (0%)
White 6 (40%) 1 (6.7%) 5 (33%) 3 (20%)
1 n (%)

Created on 2024-03-04 with reprex v2.0.2

Session info

sessionInfo()
#> R version 4.3.2 (2023-10-31 ucrt)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 19045)
#> 
#> Matrix products: default
#> 
#> 
#> locale:
#> [1] LC_COLLATE=English_United Kingdom.utf8 
#> [2] LC_CTYPE=English_United Kingdom.utf8   
#> [3] LC_MONETARY=English_United Kingdom.utf8
#> [4] LC_NUMERIC=C                           
#> [5] LC_TIME=English_United Kingdom.utf8    
#> 
#> time zone: Europe/London
#> tzcode source: internal
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] reprex_2.0.2    gtsummary_1.7.2 datapasta_3.1.0
#> 
#> loaded via a namespace (and not attached):
#>  [1] dplyr_1.1.2          compiler_4.3.2       tidyselect_1.2.0    
#>  [4] xml2_1.3.4           stringr_1.5.0        tidyr_1.3.0         
#>  [7] broom.helpers_1.14.0 yaml_2.3.7           fastmap_1.1.1       
#> [10] R6_2.5.1             commonmark_1.9.0     generics_0.1.3      
#> [13] knitr_1.43           forcats_1.0.0        tibble_3.2.1        
#> [16] pillar_1.9.0         rlang_1.1.1          utf8_1.2.3          
#> [19] stringi_1.7.12       xfun_0.39            fs_1.6.2            
#> [22] sass_0.4.6           cli_3.6.1            withr_2.5.0         
#> [25] magrittr_2.0.3       digest_0.6.32        rstudioapi_0.14     
#> [28] markdown_1.12        lifecycle_1.0.3      vctrs_0.6.3         
#> [31] evaluate_0.21        glue_1.6.2           gt_0.10.0           
#> [34] fansi_1.0.4          pacman_0.5.1         rmarkdown_2.23      
#> [37] purrr_1.0.1          tools_4.3.2          pkgconfig_2.0.3     
#> [40] htmltools_0.5.7
2 Likes

Hey @ammi.shah
Finally, I found a solution for this problem :partying_face:

It seems that gtsummary does not have any function to directly adjust the table width, so I need to convert it to a flextable object with the as_flex_table() function.

Then, we just need to use set_table_properties("autofit") to automatically adjust the width of the columns based on the content.

Try the following code and let me know if works (in my computer worked just fine! )

ps: make sure to have flextable package inside your p_load()

tbl_summary(sampledata, by = sym_resolved, percent = "row") %>% 
  as_flex_table() %>%  
  flextable::set_table_properties("autofit")
2 Likes

Hi Lucca @lnielsen ,

Thank you so much, I tried that and it works :smiley:

Thanks again!

2 Likes