How to display R data frame in generated PowerPoint
Asked Answered
O

1

5
---
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()
```

I'm working off the repro above. I'd like to present the mtcars calculated data frame in a kable or kableExtra fashion, like this:

kableExtra

Instead the table outputs in the following console format:

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

How do I make my R PowerPoint tables pretty, and even better, editable in PowerPoint?

Oporto answered 15/4, 2019 at 15:6 Comment(5)
Does this help you? #29267758Grados
@Yilun Zhang thanks for the tip. I think that only works for HTML format, not PowerPoint. I did try anyway with the YAML always_allow_html: yes but the table was more of an ASCII format table than anything resembling kable or kableExtra.Oporto
I've never had to output to PowerPoint (hope I never will!), but the PowerPoint chapter of the RMarkdown book points to the table printing section of the HTML chapter: bookdown.org/yihui/rmarkdown/…Decurrent
@Decurrent thanks that does help, unfortunately most of these HTML options don't work in PowerPoint presentations. I will use that for my normal HTML files though. Good to know.Oporto
Yeah, I'm not sure why the PowerPoint section links to the HTML table examplesDecurrent
L
6

You can use flextable package. It's support powerpoint_presentation output with R Markdown. You will find below an example :

---
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()
```
Lawhorn answered 15/4, 2019 at 16:8 Comment(1)
Hello @David Gohel I tried this but it complained about the PANDOC version, I upgraded it in the terminal (Ubuntu) but still does not work. Any idea how to work around that?Abalone

© 2022 - 2024 — McMap. All rights reserved.