rquartogtsummarygt

Table generated in quarto with gt_summary changes its format once as_gt is implemented


I am trying to create multiple tables in a pdf report using Quarto. In late 2023 the report and the tables were generated perfectly. However, I have now tried to use the same template and all the tables that have a as_gt() command get deformatted (at the header level). To be more specific, using a basic example, if I use the following command after loading the magrittr, gt and gtsummary packages:

mtcars %>%
  tbl_summary(by = cyl) 

I get a normal table when I run the code chunk and the same normal table if I render it. Basically this command allows me to use the gtsummary package to summarise the info in mtcars grouped by "cil". But if I then change my code to:

mtcars %>%
  tbl_summary(by = cyl) %>%
  as_gt() 

The table still renders normally if I run the chunk in R, but when I ask it to render to pdf, the formatting of the table's header completely changes, with the header becoming deformatted (pic below). Does anyone have any idea why this is the case? With other tables the width of the entire table is also affected.

enter image description here

The reason I need the as_gt() from the "gt" package is because I want to add specific formatting to the summary table. I have noticed that the same problem also happens if I use the command as_kable(), to convert the gtsummary table into a kable table. I have R version 4.4.1, latest Quarto and Tinytex up to date.


Solution

  • Following up on my comment above. The fix I mentioned above lives in the printing method for gtsummary tables. But when we call as_gt() explicitly, we skip the gtsummary print method, and instead the printing is handled via gt (which is why the line break fix is not present in your example). Here's how you can remove it, however.

    mtcars|>
      tbl_summary(by =cyl)|>
      modify_header(all_stat_cols()~"**{level}** N = {n}")|>
      as_gt()
    

    enter image description here

    Thanks for reporting and I'll think on how/if I can migrate that fix from the print method into the as_gt() function.