首页 > 解决方案 > Kables 没有看到我的 kable_styling 属性?

问题描述

我正在尝试并排吐出两张桌子,使用knitr::kables.

不过,我不知道如何让我的风格保持不变。如果我运行一个 kable,则样式可以正常工作:

 kable(
      caption = "Oh look! A Caption",
      starwars %>%
        count(gender, sex) %>%
        arrange(desc(gender)) 
    ) %>%
      kable_styling(bootstrap_options = c("striped", "hover", "condensed"))

给了我一张整洁的桌子: 在此处输入图像描述

但是,如果我尝试并排设置两个,格式(或kable_styling)会丢失:

knitr::kables(list(
   kable(caption = "Oh look! A Caption",
         starwars %>%
           count(gender, sex) %>%
           arrange(desc(gender))) %>%
     kable_styling(bootstrap_options = c("striped", "hover", "condensed")),
   
   
   kable(caption = "Oh look! A Caption",
         starwars %>%
           count(gender, sex) %>%
           arrange(desc(gender))) %>%
     kable_styling(bootstrap_options = c("striped", "hover", "condensed"))
 ))

格式化都消失了:

在此处输入图像描述

我如何kable_styling申请两个kables

标签: rknitrkable

解决方案


结果非常不满意的解决方案是添加第三个kable_styling()调用:

knitr::kables(list(
  kable(caption = "Species",
    starwars %>%
      count(species) %>%
      filter(n > 1)
    ) %>% kable_styling(bootstrap_options = c("striped", "hover", "condensed")),
    kable(caption = "Homeworld",
      starwars %>%
        count(homeworld) %>%
        filter(n > 1)
    ) %>% kable_styling(bootstrap_options = c("striped", "hover", "condensed"))
    
  )
) %>% kable_styling(bootstrap_options = c("striped", "hover", "condensed"))

这似乎很冗长,但它确实有效。


推荐阅读