【问题标题】:How do I get kable and formattable to work together when creating a table in R?在 R 中创建表格时,如何让 kable 和 formattable 一起工作?
【发布时间】:2019-12-19 16:52:55
【问题描述】:

kableExtra 和 formattable 都具有出色的功能,我希望能够将它们都合并到我的表格中。

这是使用 formattable 格式化的表格:

Health_status = c("Very good", "Good", "Fair", "Bad", "Very bad", "Not stated", "Total")
Number = c(1032169, 453975, 125502, 22095, 5019, 73528, 1712288)
Percent = c(60.3, 26.5, 7.3, 1.3, 0.3, 4.3, 100)
Change = c(37672, 15231, 6536, 1988, 525, 30315, 92267)
Percent_Change = c(3.8, 3.5, 5.5, 9.9, 11.7, 70.2, 5.7)
df <- data.frame(Health_status, Number, Percent, Change, Percent_Change)

df %>% 
  formattable(align = c("l", "r", "r", "r", "r"),
              list(Health_status = formatter("span", style = style(color = "grey")),
                   Change = color_bar("#71CA97")))

生成此表:

我想添加其他功能(例如表格标题),这些功能似乎无法使用 formattable 完成,但可以使用 kable。但是,当我包含一个 kable 包时,整个表丢失的是 formattble 更改:

df %>% 
  formattable(align = c("l", "r", "r", "r", "r"),
              list(Health_status = formatter("span", style = style(color = "grey")),
                   Change = color_bar("#71CA97"))) %>% 
    kable(caption = "Table1")

有没有办法只使用 formattable 来包含表格标题? (我认为没有)如果没有,有没有办法同时使用 formattable 和 kable ?

帮助表示赞赏。

【问题讨论】:

    标签: r kable formattable


    【解决方案1】:

    找到了一个使用 kableExtraformattable 的示例,这对 here 很有帮助。

    library(formattable)
    library(kableExtra)
    
    df %>% 
      mutate(
        Change = color_bar("#71CA97")(Change),
        Health_status = cell_spec(Health_status, "html", color = "grey")
      ) %>%
      kable("html", escape = F, caption = "Table 1", align = c("l", "r", "r", "r", "r")) %>%
      kable_styling("hover", full_width = F) %>%
      column_spec(4, width = "3cm")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-08
      • 1970-01-01
      • 1970-01-01
      • 2017-12-02
      • 1970-01-01
      • 2015-10-01
      相关资源
      最近更新 更多