-1

I'm trying to create a table with the gt package that I want to output to PDF.

The code works fine and looks good in RStudio, but in the rendered PDF there is no output for summary_rows().

---
title: "Untitled"
author: "author"
date: "2/28/2022"
output: pdf_document
---
```{r}
library(gt)
library(dplyr)
library(gapminder)

gapminder |> 
  group_by(continent) |> 
  count() |> 
  gt() |> 
  summary_rows(fns = list(sum = "sum"),
               formatter = fmt_number,
               decimals = 0,
               sep_mark = " ")
```

This results in a table without the summary_rows

enter image description here

─ Session info ────────────────────────────────────────────────────────────────────────────
 setting  value                       
 version  R version 4.1.0 (2021-05-18)
 os       macOS Catalina 10.15.7      
 system   x86_64, darwin17.0          
 ui       RStudio                     
 language (EN)                        
 collate  sv_SE.UTF-8                 
 ctype    sv_SE.UTF-8                 
 tz       Europe/Stockholm            
 date     2022-02-28                  

─ Packages ────────────────────────────────────────────────────────────────────────────────
 package     * version    date       lib source                         
 assertthat    0.2.1      2019-03-21 [2] CRAN (R 4.1.0)                 
 cachem        1.0.5      2021-05-15 [2] CRAN (R 4.1.0)                 
 callr         3.7.0      2021-04-20 [2] CRAN (R 4.1.0)                 
 cli           3.0.1      2021-07-17 [1] CRAN (R 4.1.0)                 
 colorspace    2.0-1      2021-05-04 [2] CRAN (R 4.1.0)                 
 crayon        1.4.1      2021-02-08 [2] CRAN (R 4.1.0)                 
 DBI           1.1.1      2021-01-15 [2] CRAN (R 4.1.0)                 
 desc          1.3.0      2021-03-05 [2] CRAN (R 4.1.0)                 
 devtools      2.4.1      2021-05-05 [2] CRAN (R 4.1.0)                 
 digest        0.6.27     2020-10-24 [2] CRAN (R 4.1.0)                 
 dplyr       * 1.0.7      2021-06-18 [1] CRAN (R 4.1.0)                 
 ellipsis      0.3.2      2021-04-29 [2] CRAN (R 4.1.0)                 
 fansi         0.5.0      2021-05-25 [2] CRAN (R 4.1.0)                 
 fastmap       1.1.0      2021-01-25 [2] CRAN (R 4.1.0)                 
 fs            1.5.0      2020-07-31 [2] CRAN (R 4.1.0)                 
 gapminder   * 0.3.0      2017-10-31 [1] CRAN (R 4.1.0)                 
 generics      0.1.1.9000 2021-12-22 [1] Github (r-lib/generics@f6668da)
 ggplot2       3.3.5      2021-06-25 [1] CRAN (R 4.1.0)                 
 glue          1.4.2      2020-08-27 [2] CRAN (R 4.1.0)                 
 gt          * 0.3.0      2021-05-12 [1] CRAN (R 4.1.0)                 
 gtable        0.3.0      2019-03-25 [2] CRAN (R 4.1.0)                 
 htmltools     0.5.2      2021-08-25 [1] CRAN (R 4.1.0)                 
 knitr         1.33       2021-04-24 [2] CRAN (R 4.1.0)                 
 lifecycle     1.0.0      2021-02-15 [2] CRAN (R 4.1.0)                 
 magrittr      2.0.1      2020-11-17 [2] CRAN (R 4.1.0)                 
 memoise       2.0.0      2021-01-26 [2] CRAN (R 4.1.0)                 
 munsell       0.5.0      2018-06-12 [2] CRAN (R 4.1.0)                 
 pillar        1.6.2      2021-07-29 [1] CRAN (R 4.1.0)                 
 pkgbuild      1.2.0      2020-12-15 [2] CRAN (R 4.1.0)                 
 pkgconfig     2.0.3      2019-09-22 [2] CRAN (R 4.1.0)                 
 pkgload       1.2.1      2021-04-06 [2] CRAN (R 4.1.0)                 
 prettyunits   1.1.1      2020-01-24 [2] CRAN (R 4.1.0)                 
 processx      3.5.2      2021-04-30 [2] CRAN (R 4.1.0)                 
 ps            1.6.0      2021-02-28 [2] CRAN (R 4.1.0)                 
 purrr         0.3.4      2020-04-17 [2] CRAN (R 4.1.0)                 
 R6            2.5.1      2021-08-19 [1] CRAN (R 4.1.0)                 
 remotes       2.4.0      2021-06-02 [2] CRAN (R 4.1.0)                 
 rlang         0.4.11     2021-04-30 [2] CRAN (R 4.1.0)                 
 rprojroot     2.0.2      2020-11-15 [2] CRAN (R 4.1.0)                 
 scales        1.1.1      2020-05-11 [2] CRAN (R 4.1.0)                 
 sessioninfo   1.1.1      2018-11-05 [2] CRAN (R 4.1.0)                 
 testthat      3.0.2      2021-02-14 [2] CRAN (R 4.1.0)                 
 tibble        3.1.6      2021-11-07 [1] CRAN (R 4.1.0)                 
 tidyselect    1.1.1      2021-04-30 [2] CRAN (R 4.1.0)                 
 usethis       2.1.5.9000 2021-12-10 [1] Github (r-lib/usethis@1bb32d7) 
 utf8          1.2.2      2021-07-24 [1] CRAN (R 4.1.0)                 
 vctrs         0.3.8      2021-04-29 [2] CRAN (R 4.1.0)                 
 withr         2.4.2      2021-04-18 [2] CRAN (R 4.1.0)                 
 xfun          0.29       2021-12-14 [1] CRAN (R 4.1.0)                 

[1] /Users/user/Library/R/x86_64/4.1/library
[2] /Library/Frameworks/R.framework/Versions/4.1/Resources/library
FilipW
  • 1,412
  • 1
  • 13
  • 25

2 Answers2

2

You will need to add your code into an R Chunk add chunk

```{r}

library(gt)
library(dplyr)
library(gapminder)

gapminder %>% 
  group_by(continent) %>% 
  count() %>% 
  gt()  %>%  
  summary_rows(fns = list(sum = "sum"),
           formatter = fmt_number,
           decimals = 0,
           sep_mark = " ")
```
Susan Switzer
  • 1,531
  • 8
  • 34
  • More details about controlling the output with each chunk at https://rmarkdown.rstudio.com/lesson-3.html – Susan Switzer Feb 28 '22 at 16:28
  • Sorry, I wasnt explicit enough. The rmarkdown document has backticks in it, I have updated the code. So the problem is not there. – FilipW Mar 01 '22 at 09:48
0

I landed here seeking similar answers.

The following for example, renders nicely in HTML:

library(tidyverse)
library(gt)

head(mtcars) |> 
  rownames_to_column("model") |> 
  gt() |> 
    tab_header(title = "Title",
               subtitle = "Subtitle") |> 
    cols_width(model ~ px(300),
             everything() ~ px(100)) |>
    tab_options(
    row.striping.include_table_body = TRUE,
    table.font.size = px(12),
    table.border.top.style = "solid",
    table.border.top.color = "white", 
    table.border.top.width = px(5),
    table.border.bottom.color = "black", 
    table.border.bottom.width = px(2),
    heading.subtitle.font.size = 12,
    heading.align = "left",
    column_labels.border.bottom.color = "black",
    column_labels.border.bottom.width = px(2)
  ) |> 
    tab_style(
    style = list(
      cell_text(align = "center", color = "white", weight = "bold"),
      cell_fill(color = "steelblue4")),
    locations = cells_column_labels(columns = everything())
  ) 

gt table knit to html

but all formatting is ignored when knitting to PDF

gt table knit to PDF

What additional configurations are required to properly knit a gt table to PDF?

fhc-cbp
  • 63
  • 4