我使用RMarkdown编写报告,并使用kable和kableExtra格式化和打印表格。这就是我想要的表格看起来像(用Word制作):
我正在努力对垂直居中(顶部和底部的"Number of Incident Occurences"
之间的空间进行排序),以便文本在行的中间对齐,留在列上。
注意,我正在创建PDF报告,而不是html。
这是一个最小的例子:
df <- data.frame(a = letters[1:5], b = 1:5)
names(df) <- c("A very very very very very very very very very very very very very very very very very very long title",
"A short title")
df %>% kable(format = 'latex', linesep = "", align = 'c', escape = F) %>% kable_styling(full_width = T)
这个输出结果如下:
垂直对齐似乎正在向顶部推进。我遇到了“m”,但只是中间对齐列。我想知道我是否需要为align = "m"
的所有行指定特殊的row_spec(),但是当我这样做时,它会抱怨....任何想法?
答案 0 :(得分:3)
似乎没有一种方法可以直接在kable和kableExtra中执行此操作。但是,当通过kable将表构建为PDF时,它使用LaTeX来构建结果。因此,我们可以将LaTeX功能直接集成到表中。
此解决方案使用Elements headers = doc.select("div[class=drug-header]");
for (Element header : headers) {
// Headers are the child of the divs.
System.out.println("Header: " + header.child(0).text());
// The content paragraphs are the next sibling of the divs.
System.out.println("Content: " + header.nextElementSibling().text());
}
包。垂直居中的单元格可以用multirow
包裹,如下所示:
\\multirow{1}{*}[0pt]{Cell content}
为了让您更轻松地使用它,您可以创建一个为您重命名的功能:
---
header-includes:
- \usepackage{multirow}
output: pdf_document
---
```{r}
library(knitr)
library(kableExtra)
df <- data.frame(a = letters[1:5], b = 1:5)
names(df) <- c("A very very very very very very very very very very very very very very very very very very long title",
"\\multirow{1}{*}[0pt]{A short title}")
df %>% kable(format = 'latex', linesep = "", align = 'c', escape = F) %>% kable_styling(full_width = T)
```
因此,要重命名您运行的列:centerText <- function(text){
paste0("\\multirow{1}{*}[0pt]{", text, "}")
}
答案 1 :(得分:0)
通过添加extra_css
参数可以很容易地在HTML中实现。 (对于那些正在寻找类似的HTML解决方案并且仅找到此帖子的人)
column_spec(1,extra_css = "vertical- align:middle;")