rlatexquartogtsummarygt

how to use gt() with long tables and pdf


GT does not seem to output long tables (breaking them over multiple pages) although it requires (recommends) the longtable latex package.

So I have been writing a report and, working in R, using gtsummary and gt to format my tables. My problem is that although gt supports latex/pdf output, any table that is longer than one page will overflow. There is seemingly no way to alter gt´s latex output (by for instance using longtable = TRUE or something)

MWE below:

    ---
    title: "Test document"
    subtitle: "TEST DOCUMENT"
    date: today
    toc: true
    format: 
      pdf:
        header-includes:
        - \usepackage{booktabs, caption, longtable, colortbl, array}
        keep-tex: true
    ---
    
    # Baseline table
    
    ```{r}
    library(tidyverse)
    library(gt)
    
    tibble(
        variable = paste0("var", 1:80),
        value = runif(80),
        test = runif(80)
    ) %>% gt()
    
    ```

Which produces this output:

output from code above

Does anyone know how this can be resolved? I would really like to use gt, but it seemingly has some quite big disadvantages. Any resources better describing gt-quarto-latex is very welcome.

EDIT: typo removed


Solution

  • Just add tab_options(latex.use_longtable = TRUE) to your {gt} object, like:

    tibble(
      variable = paste0("var", 1:80),
      value = runif(80),
      test = runif(80)
    ) %>% gt() |>
      tab_options(latex.use_longtable = TRUE)
    

    enter image description here

    See: https://gt.rstudio.com/reference/tab_options.html