英文:
kable's collapse_rows() and border
问题
请看以下的kable:
df <- data.frame(
C1 = 1:2,
C2 = c("a", "a"),
C3 = 3:4,
C4 = 5:6,
C5 = c("b", "b"),
row.names = c("行 1", "行 2")
)
df %>%
kable() %>%
add_header_above(c("", "第一组" = 2, "", "第二组" = 2),
extra_css = "border-bottom: 2px solid green;") %>%
kable_styling(full_width = FALSE) %>%
collapse_rows(columns = c(3, 6)) %>%
row_spec(0, extra_css = "border-bottom: 2px solid;") %>%
row_spec(1, extra_css = "border-bottom: 1px solid red;") %>%
row_spec(2, extra_css = "border-bottom: 2px solid blue;")
它的效果如下:
我有两个问题:
- 我想完全移除行1和行2之间的分割线。
- 我想要在行2下面有整条分割线。
我添加了红色和蓝色只是为了说明发生了什么情况。
在使用collapse_rows()
之后,row_spec()
将列C2和C5底部视为属于第1行。
所以当我编辑row_spec(1, ...)
(看红色部分),我实际上是在编辑不同行的线条。
同样的问题出现在row_spec(2, ...)
(看蓝色部分),我无法编辑已折叠行的列的线条。
我尝试使用column_spec()
,但它也不起作用。例如,
column_spec(3, extra_css = "border-bottom: 2px solid brown;")
会在C2和C3列中添加一条线,
column_spec(4, extra_css = "border-bottom: 2px solid brown;")
会在不同的行中添加线条,
column_spec(5, extra_css = "border-bottom: 2px solid brown;")
在C5下添加线条时出错。
我希望的效果如下:
我尝试使用border-bottom: none;
来消除红线部分,但线条仍然可见。
感谢您的帮助,提前致谢。
英文:
Please see the following kable:
df <- data.frame(
C1 = 1:2,
C2 = c("a", "a"),
C3 = 3:4,
C4 = 5:6,
C5 = c("b", "b"),
row.names = c("Row 1", "Row 2")
)
df %>%
kable() %>%
add_header_above(c("", "Group 1" = 2, "", "Group 2" = 2),
extra_css = "border-bottom: 2px solid green;") %>%
kable_styling(full_width = FALSE) %>%
collapse_rows(columns = c(3, 6)) %>%
row_spec(0, extra_css = "border-bottom: 2px solid;") %>%
row_spec(1, extra_css = "border-bottom: 1px solid red;") %>%
row_spec(2, extra_css = "border-bottom: 2px solid blue;")
It looks like this:
I have two problems:
- I would like to completely remove the line between Row 1 and Row 2.
- I would like to have the entire line below Row 2.
I added red and blue colors just to show what is happening.
After using collapse_rows()
, row_spec()
treats the bottom of columns C2 and C5 as belonging to row 1.
So when I edit row_spec(1, ...)
(see red color), I end up editing lines in different rows.
Same with row_spec(2, ...)
(see blue color), I cannot edit the lines of the columns with collapsed rows.
I tried to use column_spec()
, but it is also not working well. For example,
column_spec(3, extra_css = "border-bottom: 2px solid brown;")
adds a line to columns C2 and C3,
column_spec(4, extra_css = "border-bottom: 2px solid brown;")
adds lines to different rows, and
column_spec(5, extra_css = "border-bottom: 2px solid brown;")
gives an error instead of adding a line under C5.
I was expecting to have this:
I tried to use border-bottom: none;
to get rid of the red line part, but the line remains visible.
Any help appreciated, thanks in advance.
答案1
得分: 1
这应该会奏效。通过一些试错,您可以控制带有额外行和列规范的线条。似乎只有在调用"collapse rows"之前放置"column_specs"才能生效。
PS:我不得不使用kableExtra的开发版本:devtools::install_github("haozhu233/kableExtra")
,因为似乎存在一个与"collapse_rows"相关的问题:https://github.com/haozhu233/kableExtra/issues/706
英文:
This should do the trick. Using a bit of trial and error, you can control the lines with extra row and column specs. It seems the column_specs
only work if they are placed before the call to collapse rows
.
PS I had to work with the development version of kableExtra: devtools::install_github("haozhu233/kableExtra")
as there seems to be an issue with collapse_rows
: https://github.com/haozhu233/kableExtra/issues/706
library(kableExtra)
df |>
kable(fomat = "latex") |>
add_header_above(c("", "Group 1" = 2, "", "Group 2" = 2),
extra_css = "border-bottom: 2px solid green;") |>
kable_styling(full_width = FALSE) |>
column_spec(3, extra_css = "border-bottom: 2px solid;") |>
column_spec(6, extra_css = "border-bottom: 2px solid;") |>
collapse_rows(columns = c(3, 6)) |>
row_spec(0, extra_css = "border-bottom: 2px solid;") |>
row_spec(2, extra_css = "border-top: 1px solid white;") |>
row_spec(2, extra_css = "border-bottom: 2px solid;")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论