如何将大频率 table 放入 R markdown ioslides?

How to fit large frequency table into R markdown ioslides?

我正在尝试将大量 table 的频率放入我的幻灯片中。有很多值,即使是稀有的也很好显示。

我试过不同的选项,但 none 给了我一个满意的解决方案。到目前为止 Rmd

---
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 个不适合:

Table 2 可以被挤压,但字体很小,边上有很多浪费 space。

我也研究了 panderxtablestargazer,但也没有找到解决方案。

还有其他选择吗?

您可以将 table 分布在多个列中以适应 space。在下面的示例中,我将框架分成 3 对长度不均匀的列。

---
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.