Thomas W. MacFarland
Thomas W. MacFarland

Reputation: 11

The R package gtsummary and table output as simple ASCII text

I have been using the gtsummary package and find it extremely helpful, but there is one feature that I cannot find. Forgive my inattention in advance if in is included in the package vignette, but I am unable to put table output into a simple ASCII text format.

I use LaTeX for most of my work, but now and then a simple table is needed.

Do you have ideas or suggestions on how to achieve this aim?

Best wishes.

Tom MacFarland [email protected]

Upvotes: 1

Views: 367

Answers (1)

Daniel D. Sjoberg
Daniel D. Sjoberg

Reputation: 11679

Any gtsummary table can be printed with various print engines (e.g. gt, flextable, huxtable, tibble, etc). The huxtable package will print to the console by default. Does that help you out?

library(gtsummary)

trial %>%
   select(age, grade) %>%
   tbl_summary() %>%
   as_hux_table()
#>                       Characteristic     N = 200    
#>                     ────────────────────────────────
#>                       Age              47 (38, 57)  
#>                       Unknown              11       
#>                       Grade                         
#>                       I                 68 (34%)    
#>                       II                68 (34%)    
#>                       III               64 (32%)    
#>                     ────────────────────────────────
#>                       Median (IQR); n (%)           
#> 
#> Column names: label, stat_0

Another option would be to convert the gtsummary table to a tibble, and print as ASCII using the ascii package. But you lose footnote and spanning headers when you convert to a tibble.

library(gtsummary)
#> #Uighur

trial %>%
  select(age, grade) %>%
  tbl_summary() %>%
  as_tibble() %>%
  ascii::ascii()
#> |=========================================== 
#> 1.1+| h| **Characteristic** h| **N = 200** 
#> | 1 | Age                | 47 (38, 57) 
#> | 2 | Unknown            | 11          
#> | 3 | Grade              |             
#> | 4 | I                  | 68 (34%)    
#> | 5 | II                 | 68 (34%)    
#> | 6 | III                | 64 (32%)    
#> |===========================================

Created on 2021-12-07 by the reprex package (v2.0.1)

Happy Programming! I don't use ASCII in my own work. Hopefully one of these options fits into your workflow.

Upvotes: 2

Related Questions