janitor and kableExtra

I'm not sure if this belongs here, but hopefully there's a simple solution. I couldn't render a reprex due to some HTML issue, but I guess it's not THAT important in this case.

library(janitor)
library(kableExtra)
library(dplyr)

# Nice
mtcars %>% 
  tabyl(vs, gear) %>% 
  adorn_totals(c("row", "col")) %>% 
  adorn_percentages("all") %>% 
  adorn_pct_formatting() %>% 
  adorn_ns(position = "front") %>% 
  adorn_title() %>% 
  kable() %>% 
  kable_styling(bootstrap_options = c("condensed", "striped", "bordered"))

# Adding another group to tabyl()
mtcars %>% 
  tabyl(vs, gear, cyl) %>% 
  adorn_totals(c("row", "col")) %>% 
  adorn_percentages("all") %>% 
  adorn_pct_formatting() %>% 
  adorn_ns(position = "front") %>% 
  adorn_title()

# Not so nice.
mtcars %>% 
  tabyl(vs, gear, cyl) %>%
  adorn_totals(c("row", "col")) %>% 
  adorn_percentages("all") %>% 
  adorn_pct_formatting() %>% 
  adorn_ns(position = "front") %>% 
  adorn_title() %>% 
  kable() %>% 
  kable_styling(bootstrap_options = c("condensed", "striped", "bordered"))

Is there a way to keep the "native" output of janitor in section 2 while using kable? I'd like the output as rows per cyl. You'll see what I mean if you run the code..:slight_smile:

Thank you!

@bragks you can try:

library(janitor)
library(kableExtra)
library(dplyr)

tbl2 <- mtcars %>% 
  tabyl(vs, gear, cyl) %>% 
  adorn_totals(c("row", "col")) %>% 
  adorn_percentages("all") %>% 
  adorn_pct_formatting() %>% 
  adorn_ns(position = "front") %>% 
  adorn_title("combined") %>%  # use "combined"
  bind_rows(.id = "cyl") 

tbl2[-1] %>%
  kable() %>%
  kable_styling(bootstrap_options = c("condensed", "striped", "bordered")) %>%
  group_rows(index = auto_index(tbl2$cyl))
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.