如何将 Rmarkdown 中的 R gt 表定位到 pdf 页面的中心?

问题描述 投票:0回答:1

我在 Rmarkdown 文档中有一个名为

df
的数据框(pdflatex 不是 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()
  )

当我将其渲染为 pdf 时,生成的表格位于左侧,并且只有一半的表格可见。另外,如何删除 pdf 中出现的摘要警告?

enter image description here

r r-markdown gt
1个回答
0
投票

选项使用:

  1. options(gt.html_tag_check = FALSE)
  2. gt |> gtsave("pdf.png")
  3. knitr::include_graphics("pdf.png", dpi = 300)
---
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

© www.soinside.com 2019 - 2024. All rights reserved.