kable,可弹,可huxto html:强制单个线上的单元内容显示

发布于 2025-02-11 07:10:19 字数 581 浏览 2 评论 0原文

我希望我的单元格中的内容显示在一条线上。我正在将rmarkDown用于HTML。 但是,无论我使用哪个软件包(kableflextablehuxtable),都忽略了列宽度规范,并介绍了换行符,取得非常丑陋和不可读的结果。 在HTML中,有一个下拉框,总宽度不应该是问题。我只希望结果可读。

library(kableExtra)
library(flextable)

table = as.data.frame(matrix(rep("value [value1 - value2]",20), ncol = 10))

kbl(table) %>%
  kable_paper() %>%column_spec(1:ncol(table), width = "3.5cm", bold = TRUE, italic = TRUE)%>%
  scroll_box(width = "1000px", height = "500px")

tb = flextable(table)%>% flextable::width(width = 10)
knit_print(tb)

I want the contents of my cells to be displayed in a single line. I'm using Rmarkdown to HTML.
But no matter which package I use (Kable, Flextable, Huxtable), the column width specification is ignored and a line break is introduced, which makes the very ugly and unreadable results.
In HTML, with a drop-down box, the total width shouldn't be a problem. I just want the results to be readable.

library(kableExtra)
library(flextable)

table = as.data.frame(matrix(rep("value [value1 - value2]",20), ncol = 10))

kbl(table) %>%
  kable_paper() %>%column_spec(1:ncol(table), width = "3.5cm", bold = TRUE, italic = TRUE)%>%
  scroll_box(width = "1000px", height = "500px")

tb = flextable(table)%>% flextable::width(width = 10)
knit_print(tb)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉墨 2025-02-18 07:10:19

使用Flextable,此代码力(请注意autofit()的使用情况)在一行上显示显示:

library(flextable)

as.data.frame(matrix(rep("value [value1 - value2]", 20), ncol = 10)) %>% 
  flextable()%>% theme_box() %>% autofit()

这将在HTML窗口中产生表显示,此窗口的宽度有限(窗口的大小或HTML页面的最大宽度)。如果浏览器窗口的宽度小于表的宽度,则将压缩以适合窗口或可用空间。

如果您需要水平滚动(已经用于预订,但还没有用于所有HTML格式),则可以将此CSS代码添加到您的R Markdown中,以便可以滚动(即将集成到Flextable) ):

```{css echo=FALSE}
.flextable-shadow-host{
  overflow: scroll;
  white-space: nowrap;
}
```

带有它的html'r markdown'文档:

---
output: html_document
---


```{css echo=FALSE}
.flextable-shadow-host{
  overflow: scroll;
  white-space: nowrap;
}
```

```{r}
library(flextable)

as.data.frame(matrix(rep("value [value1 - value2]", 20), ncol = 10)) |> 
  flextable()|> theme_box() |> autofit()
```


With flextable, this code forces (note the usage of autofit()) the display on one single line:

library(flextable)

as.data.frame(matrix(rep("value [value1 - value2]", 20), ncol = 10)) %>% 
  flextable()%>% theme_box() %>% autofit()

This will produce a table display in an HTML window, this window has a width that is limited (the size of your window or the max-width of your HTML page). If the width of the browser window is less than the width of the table, it will be compressed to fit the window or the available space.

If you need to make this flextable horizontally scrollable (it is already implemented for bookdown but not yet for all HTML format), you can add this CSS code to your r markdown so that flextables can be scrollable (soon integrated into flextable then soon not necessary):

```{css echo=FALSE}
.flextable-shadow-host{
  overflow: scroll;
  white-space: nowrap;
}
```

An HTML 'R Markdown' document with it:

---
output: html_document
---


```{css echo=FALSE}
.flextable-shadow-host{
  overflow: scroll;
  white-space: nowrap;
}
```

```{r}
library(flextable)

as.data.frame(matrix(rep("value [value1 - value2]", 20), ncol = 10)) |> 
  flextable()|> theme_box() |> autofit()
```


enter image description here

谁的新欢旧爱 2025-02-18 07:10:19

这是huxtable等效的:

as_hux(table) |>  
  set_width(1) |> 
  set_wrap(FALSE) |>
  set_col_width("3.5cm") |> 
  quick_html()

它使桌子宽尽可能宽:

“

Here is the huxtable equivalent:

as_hux(table) |>  
  set_width(1) |> 
  set_wrap(FALSE) |>
  set_col_width("3.5cm") |> 
  quick_html()

which makes the table as wide as you want:

HTML output

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文