让 Column-Names 包裹在 R/Kable() HTML Table 中
Getting Column-Names to Wrap in R/Kable() HTML Table
我正在尝试从 R 数据框生成 HTML table 输出,但无法获取一些长列名来包装 multi-line header 排。这是我当前代码和输出的最小可重现示例:
library(datasets)
library(knitr)
library(kableExtra)
data(iris)
输出前 5 行虹膜 table 为 html 格式 table:
sink('my_file_path.html')
names_spaced <- c('Sepal Length', 'Sepal Width', 'Petal Length',
'Petal Width Long Col Name', 'Species')
kable(head(iris),
format='html',
digits=1,
row.names=FALSE,
align='lccccc',
col.names = names_spaced)
sink()
当我在浏览器中打开保存的文件时,我的 header 行只有一行,但我需要将单词换行到一两行(因此 'Petal Width Long Col Name'
)。
kable_styling
函数有一个参数 bootstrap_options
但它似乎没有我需要的东西。我也尝试在 column-names 中插入 \n
但无济于事。
如果这是解决方案的一部分,我不反对使用 xtable
包而不是 kable
/knitr
。
可以使用 HTML 语法创建换行符。为了使其工作,您必须将 kable
的 escape
参数设置为 FALSE
.
library(knitr)
data(iris)
sink('my_file_path.html')
names_spaced <- c(
'Sepal Length', 'Sepal Width', 'Petal Length',
'Petal Width<br/> Long Col Name', ## add <br/>
'Species')
kable(head(iris),
format='html',
digits=1,
row.names=FALSE,
align='lccccc',
col.names = names_spaced,
escape = FALSE) ## disable html escape to
## make <br/> work
sink()
我正在尝试从 R 数据框生成 HTML table 输出,但无法获取一些长列名来包装 multi-line header 排。这是我当前代码和输出的最小可重现示例:
library(datasets)
library(knitr)
library(kableExtra)
data(iris)
输出前 5 行虹膜 table 为 html 格式 table:
sink('my_file_path.html')
names_spaced <- c('Sepal Length', 'Sepal Width', 'Petal Length',
'Petal Width Long Col Name', 'Species')
kable(head(iris),
format='html',
digits=1,
row.names=FALSE,
align='lccccc',
col.names = names_spaced)
sink()
当我在浏览器中打开保存的文件时,我的 header 行只有一行,但我需要将单词换行到一两行(因此 'Petal Width Long Col Name'
)。
kable_styling
函数有一个参数 bootstrap_options
但它似乎没有我需要的东西。我也尝试在 column-names 中插入 \n
但无济于事。
如果这是解决方案的一部分,我不反对使用 xtable
包而不是 kable
/knitr
。
可以使用 HTML 语法创建换行符。为了使其工作,您必须将 kable
的 escape
参数设置为 FALSE
.
library(knitr)
data(iris)
sink('my_file_path.html')
names_spaced <- c(
'Sepal Length', 'Sepal Width', 'Petal Length',
'Petal Width<br/> Long Col Name', ## add <br/>
'Species')
kable(head(iris),
format='html',
digits=1,
row.names=FALSE,
align='lccccc',
col.names = names_spaced,
escape = FALSE) ## disable html escape to
## make <br/> work
sink()