如何在 rmarkdown 中使用 janitor 和 kable 垂直拆分三向表

How to split vertically three-way tables using janitor and kable in rmarkdown

当我编织这两张桌子时,它们并排出现。

如何垂直拆分它们,每组一个 c

knitr::opts_chunk$set(echo = FALSE)
library(tidyverse)
library(janitor)
library(knitr)

df <- tibble(a = c(1, 2, 1, 2, 1, 2, 1, 2),
             b = c(3, 3, 4, 3, 3, 3, 4, 4),
             c = c(1, 1, 1, 1, 2, 2, 2, 2))
df %>% tabyl(a, b, c) %>% 
  adorn_totals(c("row", "col")) %>%
  kable()

尝试purrr::walk2purrr::map对三向列表中的每个tabyls调用kable。您还需要输入

 ```{r, results='asis'}

在 RMarkdown 块中 header。

df %>% tabyl(a, b, c) %>% 
  adorn_totals(c("row", "col")) %>%
  walk2(names(.), ~ print(kable(.x, caption = .y)))

这个问题的其他方面在我链接的线程中 janitor and knitr 包的 GitHub 问题中进行了讨论。