【问题标题】:kableExtra with Formattable formatting questionskableExtra 与 Formattable 格式问题
【发布时间】:2018-03-31 20:44:33
【问题描述】:

下面的代码创建了一个小表格。 我不清楚如何进行这些更改:

1) 两个条之间的空白区域更少。使用 Inspect Element 我可以在 .table>tfoot>tr>td 中将填充从 8px 更改为 3px。这是正确的方法吗?如果是这样,如何将适当的 css 添加到我的 R 脚本中?

2) 去除彩条的圆角。同样,检查元素显示,如果我更改每个单元格的border_radius:0px 和padding-right:0px,就会发生更改。但同样,这似乎不正确。

3) 如何更改带有横条的单元格中文本的字体颜色?


library(formattable)
library(kableExtra)
library(knitr)

fraction <- function(x, df) {
  x/df$count
}

df <- tibble (
  Type = c("A", "B", "C"),
  count = c(500, 350, 860),
  Decreasing = c(226, 103, 507),
  Increasing = c(300, 250, 350)
) 


mutate(df,
       Decreasing = color_bar(color = "lightgrey", fun = "fraction", df)(Decreasing),
       Increasing = color_bar(color = "lightgreen", fun = "fraction", df)(Increasing)
) %>% 
  select(Type, Decreasing, Increasing) %>% 

  kable("html", escape = "F", align = c("l", "r", "l")) %>% 
  kable_styling(bootstrap_options = c("striped", "hover", "responsive"), full_width = F, position = "float_left")

【问题讨论】:

  • color_bar 命令未加载 - 您是否错过了对 library 的调用?
  • 对不起。请尝试图书馆(knitr)
  • knitr 没有color_bar 命令。
  • color_bar {formattable}

标签: css r hugo kableextra formattable


【解决方案1】:

color_bar 来自formattable 包,但好消息是您可以定义自己的color_bar 函数(只需在R 控制台中输入color_bar 即可获得color_bar 的源代码,然后你可以修改它)。它将解决您的问题 2 和 3。

color_bar2 <- function (color = "lightgray", fun = "proportion", ...) 
{
  fun <- match.fun(fun)
  formatter(
    "span", 
    style = function(x) style(
      display = "inline-block", 
      direction = "rtl", `border-radius` = "0px", `padding-right` = "2px", 
      `background-color` = csscolor(color), color = csscolor("red"),
      width = percent(fun(as.numeric(x), ...))))
}

mutate(df,
       Decreasing = color_bar2(color = "lightgrey", fun = "fraction", df)(Decreasing),
       Increasing = color_bar2(color = "lightgreen", fun = "fraction", df)(Increasing)
) %>% 
  select(Type, Decreasing, Increasing) %>% 

  kable("html", escape = "F", align = c("l", "r", "l")) %>% 
  kable_styling(bootstrap_options = c("striped", "hover", "responsive"), full_width = F, position = "float_left")

对于问题 1,如果您在 rmarkdown 中呈现表格,请查看此页面 https://rmarkdown.rstudio.com/html_document_format.html#custom_css,了解如何在 rmarkdown 中使用 custom_css。

【讨论】:

  • 感谢您提醒我这一点。很高兴知道现在和将来。
【解决方案2】:

如果您不介意不同的包(我自己的):

library(tibble)
library(dplyr)
library(huxtable)
df <- tibble (
  Type = c("A", "B", "C"),
  count = c(500, 350, 860),
  Decreasing = c(226, 103, 507),
  Increasing = c(300, 250, 350)
) 

ht <- as_hux(df) 
ht %>% 
       select(Type, Decreasing, Increasing) %>% 
       set_all_padding('3px') %>% 
       set_text_color(everywhere, 2:3, "red") %>% 
       add_colnames()

不过,这不会让您获得彩条。 (嗯,也许我应该添加一个功能。)您可以通过...来获得这些功能:

my_color_bar <- color_bar(color = "lightgrey", fun = "fraction", df)

ht %>% 
  select(Type, Decreasing, Increasing) %>% 
  set_all_padding('3px') %>% 
  set_text_color(everywhere, 2:3, "red") %>% 
  set_escape_contents(everywhere, 2:3, TRUE) %>% 
  set_number_format(everywhere, 2:3, list(my_color_bar)) %>% 
  add_colnames()

【讨论】:

    猜你喜欢
    • 2018-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多