下面的代码创建一个小表。我不清楚如何作出这些改变:
1)两条之间的空白较小。使用检查元素,我可以在.table>tfoot>tr>td中将填充从8px更改为3px。这是正确的方法吗?如果是这样,我如何将适当的css添加到我的R脚本中?
2)移除色条的四舍五入。检查元素再次显示,如果更改每个单元格的border_radius:0px和填充-右: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")发布于 2018-04-03 00:46:38
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")对于第一个问题,如果您在rmarkdown中呈现您的表,请查看此页面css,以了解如何在rmarkdown中使用custom_css。
发布于 2018-03-31 21:56:55
如果你不介意换一个包(我自己的):
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()https://stackoverflow.com/questions/49592235
复制相似问题