在表 - Kable上改变边框的边框颜色

时间:2022-07-30 20:29:26

I am trying to create borders around specific columns in R. I am able to do this using the kableExtra package (shown below)

我正在尝试在R中的特定列周围创建边框。我可以使用kableExtra包执行此操作(如下所示)

iris <- datasets::iris

iris2 <- do.call(data.frame, aggregate(. ~ Species, iris, function(x) c(mean = mean(x), sd = sd(x))))

res2 <- kable(iris2,"html", col.names =  c("Species", "Mean", "SD", "Mean", "SD", "Mean", "SD", "Mean", "SD"),   align = c("r","c","c","c","c","c","c","c","c")) %>%
  kable_styling(bootstrap_options = c("striped", "hover")) %>%
  add_header_above(c(" " = 1, "Sepal Length" = 2, "Sepal Width" = 2, "Petal Length" = 2, "Petal Width" = 2))

print(res2)

res3<- column_spec(res2, c(2,4,6,8), border_left = TRUE, border_right = FALSE)

column_spec(res3, c(3,5,7,9), border_left = FALSE, border_right = TRUE)

This creates the border I would like in the right positions, but I would like them to be gray and not black. I tried using the 'color=' from the column_spec function but it turns the whole column gray and not just the border.

这会在正确的位置创建我想要的边框,但我希望它们是灰色而不是黑色。我尝试使用column_spec函数中的'color =',但它将整个列变为灰色而不仅仅是边框。

res3<- column_spec(res2, c(2,4,6,8), border_left = TRUE, border_right = FALSE, color = "#D8D7D7")
column_spec(res3, c(3,5,7,9), border_left = FALSE, border_right = TRUE, color = "#D8D7D7")

Any suggestions on how to change the border color would be helpful! For now, I am using getAnywhere() to see if I can just change the function slightly.

有关如何更改边框颜色的任何建议都会有所帮助!现在,我正在使用getAnywhere()来查看是否可以稍微更改该函数。

1 个解决方案

#1


8  

Use css into a border. This works

将css用于边框。这很有效

column_spec(res3, c(3,5,7,9), border_left = "2px solid gray",
            border_right = "2px solid gray")

#1


8  

Use css into a border. This works

将css用于边框。这很有效

column_spec(res3, c(3,5,7,9), border_left = "2px solid gray",
            border_right = "2px solid gray")