How to display R data frame in generated PowerPoint - r

---
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:
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?

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

Related

add "continued" to long table caption using flextable in docx r

I knit code below. and this is the output. long in kbl(0 for html, I want to have the term "continued" in flextable for docx too. what is the trick?
---
title: ''
output: word_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r echo=FALSE, message=FALSE, warning=FALSE}
library(flextable)
library(dplyr)
library(ggplot2)
mpg[, 1:4] %>%
flextable() %>%
set_caption(caption = "my caption") %>%
set_table_properties(width = 1, layout = "autofit")
```

Set internal document link as table value in Rmarkdown

I would like to set each value of a column in a datatable as an internal link to a different slide. Below is what I have tried but the link is not selectable.
---
title: "Example"
output: slidy_presentation
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
```{r message=FALSE, warning=FALSE, include=FALSE}
library(DT)
library(tidyverse)
```
## Table
```{r carstab, echo = FALSE}
datatable(mtcars %>% rownames_to_column(var = "plot") %>% mutate(plot = ("[plot](#/plot)")),escape = TRUE)
```
## plot
```{r plot, echo = TRUE}
plot(mtcars)
```
mutate(plot = ("[plot](#/plot)")) does not work because the markdown code [plot](#/plot) will not be translated to HTML. Its a character.
You want something like <a href = '#(slide_number)'> This is a link </a> in your DT cells.
You could generate such links using paste0():
datatable(
mtcars %>%
mutate(
plot = 3,
plot = paste0("<a href = '#(", plot, ")'>link</a>"),
), escape = FALSE
)
The plot column in your DT now contains valid links to slide 3 (modify the first instance of plot in mutate() accordingly to link to different slides).

R markdown: how to print dataframe compactly

I am using knitr::kable to print my dataframes, but sometimes they are too big. Is there any simple way to print them compactly with scrollbar?
For example, I do:
knitr::kable(mtcars)
How could I add scrolling by condition (for example, if nrow > 10 and/or ncol > 10)?
P.S. DT::datatable doesn't work for big ncol:
I need exactly scrolling interface.
You can add scrollbars. For example, with kableExtra or DT:
R Markdown
---
title: "Untitled"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(kableExtra)
```
Some very wide data:
```{r}
df <- cbind(mtcars, mtcars)
```
With `kableExtra`:
```{r}
kable(df) %>%
kable_styling("striped", full_width = F) %>%
scroll_box(width = "100%", height = "200px")
```
Or with `DT`:
```{r}
DT::datatable(
df,
height = 200,
options = list(scrollX = TRUE)
)
```
output

rmarkdown: kable, xtable or tab_df tables in Word doc

---
output:
word_document: default
---
```{r setup, include=FALSE}
data("mtcars")
library(tidyverse)
library(xtable)
library(sjPlot)
library(kableExtra)
```
```{r, results='asis'}
df <- mtcars %>%
group_by(cyl) %>%
summarise(disp = mean(disp),
wt = mean(wt),
n = n()
)
kable(df)
# tab_df(df)
# xtable(df)
```
I have tried xtable, tab_df, and kable to generate a word document with a table. When "knit to HTML document", all tables looked fine. When "knit to Word", xtable didn't show the table while tab_df and kable produced a table with only one column:
kable(df)
cyl
disp
wt
n
4
105.1364
2.285727
I experimented with flextable quite a bit the last few days and it might be the best option when you have to work with Word:
---
output:
word_document: default
---
```{r setup, include=FALSE}
data("mtcars")
library(tidyverse)
library(flextable)
```
```{r, results='asis'}
mtcars %>%
group_by(cyl) %>%
summarise(disp = mean(disp),
wt = mean(wt),
n = n()
) %>%
flextable() %>%
align(part = "all") %>% # left align
set_caption(caption = "Table 1: Example") %>%
font(fontname = "Calibri (Body)", part = "all") %>%
fontsize(size = 10, part = "body") %>%
# add footer if you want
# add_footer_row(values = "* p < 0.05. ** p < 0.01. *** p < 0.001.",
# colwidths = 4) %>%
theme_booktabs() %>% # default theme
autofit()
```

How to fit large frequency table into R markdown ioslides?

I'm trying to fit large table of frequencies into my slide. There are many values, and even the rare ones would be nice to show.
I played with different options but none gives me a satisfactory solution. Here is Rmd so far:
---
title: "Untitled"
author: "author"
date: "date"
output: ioslides_presentation
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
df <- as.data.frame(table(rownames((USArrests))))
```
## table 1
```{r t1, echo = TRUE}
table(rownames((USArrests)))
```
## table 2
```{r t2}
library(knitr)
library(kableExtra)
kable(df, "html") %>%
kable_styling(bootstrap_options = "striped", font_size = 10)
```
Table 1 doesn't fit:
Table 2 could be squeezed but with tiny font, and lots of wasted space on the sides.
I also looked into pander, xtable and stargazer but failed to find solution from them either.
Any other alternatives?
You could spread your table across multiple columns to fit the space. In my example below, I split the frame up into 3 pairs of columns with uneven length.
---
output: ioslides_presentation
---
```{r setup, include=FALSE}
library(dplyr)
library(magrittr)
library(knitr)
library(kableExtra)
```
## table 1
```{r, echo=TRUE, eval=FALSE}
USArrests %>% rownames %>% table
```
```{r, echo=FALSE}
df <- USArrests %>%
rownames %>%
table %>%
as_tibble
df %$%
tibble(
name1 = `.`[1:17],
n1 = n[1:17],
name2 = `.`[18:34],
n2 = n[18:34],
name3 = c(`.`[35:50], ""),
n3 = c(n[35:50], "")
) %>%
kable("html", align = c("l", "c"), col.names = rep(c("Name", "Frequency"), 3)) %>%
kable_styling(bootstrap_options = c("striped", "condensed"), font_size = 18)
```
N.B. I accept the transform step into multiple columns could have been done more elegantly and providing and more programmatic solution, however, I'll leave that to others to refine.

Resources