使html表列更宽/防止单词在RMarkdown中换行 [英] Make html table columns wider / prevent words from wrapping in RMarkdown
问题描述
我正在创建带有R降价的html文档,其中包含一些表格.在表的一列具有字符值的情况下,如何确保该列足够宽以包含整个字符串而无需换行?我尝试在下面使用kableExtra
,但即使bold
参数被使用,似乎也没有使用column_spec
的width
参数.
I'm creating an html document with R markdown which has some tables. In situations where one column of a table has character values, how can I ensure the column is wide enough to contain the entire character string without wrapping? I tried using kableExtra
below, but it seems like the width
argument of column_spec
is not being used even though the bold
argument is.
library(data.table)
csd <- fread(' Oct-17 Sep-17 Aug-17 Jul-17 Jun-17 May-17 Apr-17 Mar-17 Feb-17 Jan-17 Dec-16 Nov-16 Oct-16 Sep-16 2017 YTD 2016 YTD 2015 YTD
V1
V2 71,687 74,492 72,772 74,785 77,084 72,819 85,367 77,403 85,131 81,585 80,186 89,810 92,871 691,540 1,141,589 1,207,433
V3 22,788 22,355 23,093 23,239 23,821 23,005 25,883 22,168 24,812 23,715 22,708 28,128 29,366 211,164 353,006 411,659
V4 #DIV/0! 31.8% 30.0% 31.7% 31.1% 30.9% 31.6% 30.3% 28.6% 29.1% 29.1% 28.3% 31.3% 31.6% 30.5% 30.9% 34.1%
Some long variable name 30,047 31,910 30,046 31,766 33,455 30,913 37,524 33,683 37,589 36,571 35,590 44,447 44,295 296,933 516,597 528,305
V5 2.89% 1.83% 1.55% 1.97% 2.85% 1.37% 4.95% 5.54% 3.45% 3.12% 1.92% 2.65% 1.69% 3.01% 2.04% 0.61%
V6 867 583 465 626 952 422 1,857 1,866 1,298 1,140 682 1,179 748 8,936 10,539 3,201
V7 29,180 31,327 29,581 31,140 32,503 30,491 35,667 31,817 36,291 35,431 34,908 43,268 43,547 287,997 506,058 525,104
V8 0:23 0:15 0:10 0:20 0:29 0:14 0:53 1:03 0:33 0:24 0:20 0:25 0:17 0:29 0:21 0:06
V9 4:53 4:44 4:46 5:00 5:01 5:05 5:01 5:05 5:01 4:57 5:01 4:49 4:52 4:57 4:47 4:11
V10 86% 91% 94% 89% 83% 91% 78% 72% 81% 86% 89% 85% 92% 85% 89% 94%
V11 99.05% 98.20% 96.40% 97.25% 97.80% 96.50% 95.55% 95.85% 95.65% 96.25% 96.55% 97.75% 97.95% 96.92% 97.33% 98.23%
V12 99.75% 100.00% 99.90% 98.85% 99.00% 98.75% 99.00% 99.55% 99.85% 99.45% 99.20% 97.70% 97.55% 99.41% 98.50% 99.01%
')
csd <- csd[-1,-2]
names(csd)[1] <- 'V0'
words <- c('these','are','some','words','extreme','slightly')
csd[,1] <- replicate(nrow(csd), paste(sample(words, 7, T), collapse = " "))
library(knitr)
library(magrittr)
library(kableExtra)
csd %>%
kable('html', digits = 2) %>%
column_spec(1, bold = T, width = "2600em") %>%
kable_styling(bootstrap_options = c("striped", "hover"))
推荐答案
我认为这更像是HTML/css问题.当HTML表的宽度超过页面宽度时,如有必要,它将尝试减小列宽.如果减少csd
的列数,您将看到width
选项开始显示效果.
I think it's more like an HTML/css issue. When the width of an HTML table exceed the page width, it will try to reduce column width if not necessary. If you reduce the number of columns for csd
, you will see the width
option starts to show an effect.
在这种情况下,一种解决方法是使用scroll_box
函数并为表格提供更大的绘图区域.
In this case, one way to work around is to use the scroll_box
function and give the table a wider plotting area.
csd %>%
kable('html', digits = 2) %>%
column_spec(1, bold = T, width = "2600em") %>%
kable_styling(bootstrap_options = c("striped", "hover")) %>%
scroll_box(width = "2000px")
更新
另一种骇人听闻但真实"的解决方案是强制将单元格显示为嵌入式块
Another hacky but "real" solution is to force the cell display as an inline-block
csd %>%
kable('html', digits = 2) %>%
column_spec(1, bold = T, width = "2600em; display: inline-block;") %>%
kable_styling(bootstrap_options = c("striped", "hover"))
更新
display: inline-block
现在默认包含在kableExtra(dev ver)中.
The display: inline-block
thing is now included by default in kableExtra (dev ver).
如果默认情况下为display: inline-block;
,则表列的标题行将失去自动调整其宽度的能力.当表又小又紧时,它会破坏column_spec.结果,我将默认删除此行.如果需要强行设置宽度.您可以随时使用我上面提供的方法.
If we have display: inline-block;
by default, the header row of the table columns lost the ability to adjust its width automatically. It breaks column_spec when the table is small and tight. As a result, I'm removing this line from default. If you need to forcefully set the width. you can always use the method I provided above.
这篇关于使html表列更宽/防止单词在RMarkdown中换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!