0

I want to caption the table at the top/bottom of a table. Is there a caption function in the gt package?

 gt(head(mtcars)) %>%
        tab_header(title="", subtitle="Table 1: Mtcars with gt.") %>%
        tab_options(table.width=pct(90),
                        table.border.top.style=0,
                        row.padding=px(4)) %>%
            tab_style(style=cells_styles(
                          text_align='left',
                          text_color='grey'),
                  locations=cells_title(groups="subtitle"))
Ed_Gravy
  • 1,841
  • 2
  • 11
  • 34

2 Answers2

1

I could get the caption argument by installing the development version of the package from Github.

#devtools::install_github("rstudio/gt")
library(gt)

gt(head(mtcars), caption = 'This is table caption') %>%
  tab_header(title="A", subtitle="Table 1: Mtcars with gt.")

enter image description here

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
1

It seems strange and inconsistent (and annoying!) to not have an alignment option for caption. Especially since gtsummary uses "caption" instead of "title" for table titles.

I found this workaround in another thread (Format caption of gtsummary tables). The idea is to enter the caption as html and do the formatting there.

"<div style='text-align: left; font-weight: bold; color: grey'> Table 1. Patient Characteristics</div>"
MikeS
  • 103
  • 7