Home > Software design >  How to split vertically three-way tables using janitor and kable in rmarkdown
How to split vertically three-way tables using janitor and kable in rmarkdown

Time:10-05

When I knit these two tables they appear side by side.

What can I do to split them vertically, one for each group of 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()

CodePudding user response:

Try purrr::walk2 or purrr::map to call kable on each of the tabyls in the 3-way list. You'll also need to put

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

in the RMarkdown chunk header.

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

enter image description here

Other aspects of this problem are discussed in the GitHub issues for both the janitor and knitr packages in the threads I linked.

  • Related