Search code examples
rr-markdowngt

How can i position to the center of a pdf page a gt table in R from Rmarkdown?


I have a data frame called df in an Rmarkdown document (pdflatex not xelatex).

---
title: "t2"
output: pdf_document
date: "2024-04-30"
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
suppressMessages(library(tidyverse))
suppressMessages(library(knitr))
suppressMessages(library(gt))
suppressMessages(library(rmarkdown))
```


```{r,echo=FALSE}
df = structure(list(projects = c("Restaurant, Pizzeria and Pasta House Works for  New York",
                                 "London Project - New Restaurant  Project", 
                                 "Athens - Beautiful Lands for Aigaleo City Project",
                                 "Berlin City - Exhibition  near the airport with restaurants",
                                 "Pizzeria Buenos Aires New italian restaurant - pasta, tartufo and vino",
                                 "area mean value", "total mean value"), 
                    happiness.and.joyfullness = c(3.5,4, 3, 3.2, 4, 5, 3), 
                    joy.for.children = c(3, 5, 3, 4, 5,4, 4), 
                    area = c(3, 5, 3, 3, 3, 4, 4), 
                    damages.from.environment = c(2,4, 2, 3, 3, 5, 5), 
                    approach  = c(3, 1, 5,3, 5, 5, 4), 
                    expensive = c(3, 5, 3, 4, 5, 5, 5), 
                    safety.comes.first = c(5,5, 5, 5, 5, 5, 4),
                    hungry = c(3, 5, 2, 4, 5,5, 5)), 
               row.names = c(NA, -7L), 
               class = c("tbl_df", "tbl","data.frame"))

df %>%
  gt() %>%
  cols_label("happiness.and.joyfullness" = md("happiness<br>and<br>joyfullness")) %>%
  cols_label("joy.for.children" = md("joy<br>for<br>children")) %>%
  cols_label("damages.from.environment" = md("damages<br>from<br>environment")) %>%
  cols_label("safety.comes.first" = md("safety<br>comes<br>first")) %>% 
  tab_style(
    style = "vertical-align:middle; font-weight: bold",
    locations = cells_column_labels()
  )

When I render it to pdf, the resulting table is positioned to the left and only half of the table is visible. Also, how can I remove the summarise warning that appears in the pdf?

enter image description here


Solution

  • Option using:

    1. options(gt.html_tag_check = FALSE)
    2. gt |> gtsave("pdf.png")
    3. knitr::include_graphics("pdf.png", dpi = 300)

    (Note, the webshot2 package must be installed before saving any table as an image file.)

    ---
    title: "t2"
    output: pdf_document
    date: "2024-04-30"
    ---
    
    ```{r}
    
    knitr::opts_chunk$set(echo = TRUE)
    suppressMessages(library(tidyverse))
    suppressMessages(library(knitr))
    suppressMessages(library(gt))
    suppressMessages(library(rmarkdown))
    options(gt.html_tag_check = FALSE)
    ```
    
    ```{r}
    #| output: as-is
    
    df = structure(list(projects = c("Restaurant, Pizzeria and Pasta House Works for  New York",
                                     "London Project - New Restaurant  Project", 
                                     "Athens - Beautiful Lands for Aigaleo City Project",
                                     "Berlin City - Exhibition  near the airport with restaurants",
                                     "Pizzeria Buenos Aires New italian restaurant - pasta, tartufo and vino",
                                     "area mean value", "total mean value"), 
                        happiness.and.joyfullness = c(3.5,4, 3, 3.2, 4, 5, 3), 
                        joy.for.children = c(3, 5, 3, 4, 5,4, 4), 
                        area = c(3, 5, 3, 3, 3, 4, 4), 
                        damages.from.environment = c(2,4, 2, 3, 3, 5, 5), 
                        approach  = c(3, 1, 5,3, 5, 5, 4), 
                        expensive = c(3, 5, 3, 4, 5, 5, 5), 
                        safety.comes.first = c(5,5, 5, 5, 5, 5, 4),
                        hungry = c(3, 5, 2, 4, 5,5, 5)), 
                   row.names = c(NA, -7L), 
                   class = c("tbl_df", "tbl","data.frame"))
    
    gt <- df %>%
      gt() %>%
      cols_label("happiness.and.joyfullness" = md("happiness<br>and<br>joyfullness")) %>%
      cols_label("joy.for.children" = md("joy<br>for<br>children")) %>%
      cols_label("damages.from.environment" = md("damages<br>from<br>environment")) %>%
      cols_label("safety.comes.first" = md("safety<br>comes<br>first")) %>% 
      tab_style(
        style = "vertical-align:middle; font-weight: bold",
        locations = cells_column_labels()
      )
    
    gt |> gtsave("pdf.png")
    
    knitr::include_graphics("pdf.png", dpi = 300)
    ```
    

    enter image description here