RMarkdown `gt` Package Rendering Different in HTML and PDF

Hello, I am having some issues rendering gt tables while using RMarkdown. The tables print differently when I render them as HTML or PDF documents.

For example when I run the code below

date <-  c('2000-01-01',
           '2000-01-01',
           '2000-01-01',
           '2000-01-01',
           '2000-01-01')
value <- c(23, 41, 32, 58, 26)
df <- data.frame(date, value)

library(gt)

gt(df)  %>%
  cols_label(date = 'Date',
             value = 'Value') %>%
  cols_align(align = 'center') %>%
  tab_source_note(
    source_note = 'Source: ABC Company'
    ) %>%
  tab_options(
    column_labels.border.top.color = 'black',
    column_labels.border.top.style = 'solid',
    column_labels.border.top.width = 0.5,
    table.border.bottom.color = 'black',
    table.border.bottom.style = 'solid',
    table.border.bottom.width = 0.5,
    table.border.left.color = 'black',
    table.border.left.style = 'solid',
    table.border.left.width = 0.5,
    table.border.right.color = 'black',
    table.border.right.style = 'solid',
    table.border.right.width = 0.5,
  )

as an HTML document (output: html_document) , I get the table on the top, and when I run the code as a PDF document (output: pdf_document), I get the table on the bottom.

Can someone please help me with this? I would like the PDF table to have the same formatting as the HTML table.

I found a thread with this same issue, but didn't find any solutions Using gt in Markdown and creating a PDF.

Thank you in advance!

This is more of a feature than a bug. When rendering as a PDF document, what we are really doing is rendering a LaTeX table and delivering it as a PDF doc. With HTML, gt is constructing an HTML table and displaying it in a browser. Consequently, at this time, LaTeX tables in gt don't support the full set of styling options available in HTML tables.

If you like the look of the HTML table and want that in your LaTeX document, a reasonable option is to save an HTML table as a PNG graphic (with the gtsave() function) and use that PNG output in your LaTeX/PDF doc with knitr's include_graphics() function.

3 Likes

Ah I see. Thank you for your help! Ya I am planning on using ggsave() and knitr::include_graphics() to work around this problem when rendering PDFs.

Guess I should have known considering the R Markdown Cookbook explicitly says "Currently gt mainly supports HTML output". ¯\ _(ツ)_/¯

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.