kable的collapse_rows()和border

huangapple go评论74阅读模式
英文:

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;")

它的效果如下:

kable的collapse_rows()和border

我有两个问题:

  • 我想完全移除行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下添加线条时出错。

我希望的效果如下:

kable的collapse_rows()和border

我尝试使用border-bottom: none;来消除红线部分,但线条仍然可见。

感谢您的帮助,提前致谢。

英文:

Please see the following kable:

df &lt;- data.frame(
  C1 = 1:2, 
  C2 = c(&quot;a&quot;, &quot;a&quot;), 
  C3 = 3:4, 
  C4 = 5:6, 
  C5 = c(&quot;b&quot;, &quot;b&quot;), 
  row.names = c(&quot;Row 1&quot;, &quot;Row 2&quot;)
  )

df %&gt;% 
  kable() %&gt;%
  add_header_above(c(&quot;&quot;, &quot;Group 1&quot; = 2, &quot;&quot;, &quot;Group 2&quot; = 2), 
                   extra_css = &quot;border-bottom: 2px solid green;&quot;) %&gt;%
  kable_styling(full_width = FALSE) %&gt;%
  collapse_rows(columns = c(3, 6)) %&gt;% 
  row_spec(0, extra_css = &quot;border-bottom: 2px solid;&quot;) %&gt;%
  row_spec(1, extra_css = &quot;border-bottom: 1px solid red;&quot;) %&gt;%
  row_spec(2, extra_css = &quot;border-bottom: 2px solid blue;&quot;)

It looks like this:

kable的collapse_rows()和border

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 = &quot;border-bottom: 2px solid brown;&quot;)

adds a line to columns C2 and C3,

column_spec(4, extra_css = &quot;border-bottom: 2px solid brown;&quot;)

adds lines to different rows, and

column_spec(5, extra_css = &quot;border-bottom: 2px solid brown;&quot;)

gives an error instead of adding a line under C5.

I was expecting to have this:

kable的collapse_rows()和border

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(&quot;haozhu233/kableExtra&quot;) as there seems to be an issue with collapse_rows: https://github.com/haozhu233/kableExtra/issues/706

library(kableExtra)

  df |&gt; 
  kable(fomat = &quot;latex&quot;) |&gt;
  add_header_above(c(&quot;&quot;, &quot;Group 1&quot; = 2, &quot;&quot;, &quot;Group 2&quot; = 2), 
                   extra_css = &quot;border-bottom: 2px solid green;&quot;) |&gt;
  kable_styling(full_width = FALSE) |&gt;
  column_spec(3, extra_css = &quot;border-bottom: 2px solid;&quot;) |&gt; 
  column_spec(6, extra_css = &quot;border-bottom: 2px solid;&quot;) |&gt; 
  collapse_rows(columns = c(3, 6)) |&gt; 
  row_spec(0, extra_css = &quot;border-bottom: 2px solid;&quot;) |&gt; 
  row_spec(2, extra_css = &quot;border-top: 1px solid white;&quot;) |&gt;
  row_spec(2, extra_css = &quot;border-bottom: 2px solid;&quot;) 


kable的collapse_rows()和border

huangapple
  • 本文由 发表于 2023年6月13日 11:52:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76461593.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定