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

时间:2019-04-15 15:06:47

标签: r r-markdown powerpoint kableextra officer

---
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计算出的数据帧,如下所示:

kableExtra

该表将以以下控制台格式输出:

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

我如何使我的R PowerPoint表变得漂亮甚至更好,可以在PowerPoint中进行编辑?

1 个答案:

答案 0 :(得分:2)

您可以使用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()
```