gpt4 book ai didi

css - R Markdown : nice way to show a long text in a html table

转载 作者:行者123 更新时间:2023-11-28 08:54:22 24 4
gpt4 key购买 nike

假设我想在 R markdown 文件中显示此表:

test.df <- data.frame(A = 'test', B = 'wow', C = paste0(1:10000, collapse = ', '))
kable_styling(kable( test.df, caption="test df" ))

然而,这会使 C 列变得巨大,当我将其导出到 html 文件时,这看起来很难看,尤其是在有更多行的情况下。

enter image description here

有没有办法调整样式,使表格看起来更整洁,例如:

  • 在带有滚动条的文本框中显示列 C,滚动条定义了 width x height
  • 使 C 列中的文本在用户单击“展开”之类的内容时折叠并可见
  • 欢迎提出其他建议;)

最佳答案

也许最直接的解决方案是使用 CSS。

kableExtra::cell_spec() 函数可用于将单元格内容嵌入到 span 元素中。将此跨度显示为 block 并设置宽度和高度应该足以添加滚动条。

这是一个可重现的例子:

---
output: html_document
---

```{r, echo=FALSE, message=FALSE}
test.df <- data.frame(A = 'test', B = 'wow', C = paste0(1:10000, collapse = ', '))
test.df$C <- kableExtra::cell_spec(
test.df$C,
extra_css = "display: block; overflow: scroll; height: 5em; width: 20em;"
)

kableExtra::kable_styling(knitr::kable(test.df, caption="test df", escape = FALSE))
```

关于css - R Markdown : nice way to show a long text in a html table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58150864/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com