如何在生成的 PowerPoint 中显示 R 数据框

How to display R data frame in generated PowerPoint

---
title: "Untitled"
output: powerpoint_presentation
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```

## Table

```{r table, echo=FALSE, message=FALSE, warning=FALSE}
library(tidyverse)
library(kableExtra)
mtcars %>% 
  count(cyl) %>% 
  ungroup()  # %>%
  # kable() %>%
  # kable_styling()
```

我正在处理上面的重现。我想以 kable 或 kableExtra 方式呈现 mtcars 计算数据框,如下所示:

相反,table 以以下控制台格式输出:

## # A tibble: 3 x 2
##     cyl     n
##   <dbl> <int>
## 1     4    11
## 2     6     7
## 3     8    14

如何使我的 R PowerPoint table在 PowerPoint 中更漂亮,甚至更好,editable?

您可以使用 flextable 包。 R Markdown 支持 powerpoint_presentation 输出。您将在下面找到一个示例:

---
title: "Untitled"
output: powerpoint_presentation
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```

## Table

```{r table, echo=FALSE, message=FALSE, warning=FALSE}
library(magrittr)
library(flextable)
mtcars[1:5,1:4] %>% 
  tibble::rownames_to_column() %>% 
  flextable() %>%
  set_header_labels(rowname = "") %>% 
  add_header_row(values = c("", "Group 1", "Group 2"), 
                 colwidths = c(1, 2, 2)) %>% 
  theme_zebra() %>% autofit()
```