r - 在 R flextable 中,如何在列标题之间添加分隔符
问题描述
我有一个包含嵌套列标题的列联表,我需要帮助在列标题之间添加空格。有三个组织学,每个内部都是三个阶段。我想在三组舞台之间休息一下。我做到了这一点:
Cancer <- read.table("http://users.stat.ufl.edu/~aa/cat/data/Cancer.dat",
header = TRUE, stringsAsFactors = TRUE)
library(dplyr)
library(tidyr)
cancerCountWide <- Cancer %>%
select(-risktime) %>%
pivot_wider(id = time, names_from = c(histology, stage),
values_from=count) %>%
mutate(blank = " ") %>%
mutate(blank2 = " ") %>%
select(time, blank, `1_1`, `2_1`, `3_1`, blank2, everything())
my_header <- data.frame(
col_keys = c("time", "blank", "1_1", "2_1", "3_1", "blank", "1_2", "2_2", "3_2", "1_3", "2_3","3_3"),
line2 = c("Follow-up", "Histology", rep("I", 3), "blank", rep("II", 3), rep("III", 3)),
line3 = c("Follow-up", "Disease Stage", c(1,2,3), "blank", rep(c(1,2,3),2))
)
library(flextable)
flextable(cancerCountWide) %>%
set_header_df(
mapping = my_header,
key = "col_keys"
) %>%
theme_booktabs() %>%
merge_v(part = "header") %>%
merge_h(part = "header") %>%
align(align = "center", part = "all") %>%
autofit() %>%
empty_blanks()
这让我很接近:
添加空白列不是很优雅,阶段下方的细线也不好。我认为必须有更好的方法来做到这一点......
有人可以建议我如何在舞台组之间添加一些填充吗?我是 flextable 的新手。因此,对于学习教程的建议也将不胜感激。
解决方案
我会做:
Cancer <- read.table("http://users.stat.ufl.edu/~aa/cat/data/Cancer.dat",
header = TRUE, stringsAsFactors = TRUE)
library(dplyr)
library(tidyr)
library(scales)
library(flextable)
cancerCountWide <- Cancer %>%
select(-risktime) %>%
pivot_wider(id = time, names_from = c(histology, stage),
values_from=count) %>%
mutate(`histo` = " ") %>%
select(time, histo, `1_1`, `2_1`, `3_1`, everything())
my_header <- data.frame(
col_keys = c("time", "histo", "blank1", "1_1", "2_1", "3_1", "blank2", "1_2", "2_2", "3_2", "blank3", "1_3", "2_3","3_3"),
line2 = c("Follow-up", "Histology", "", rep("I", 3), "", rep("II", 3), "", rep("III", 3)),
line3 = c("Follow-up", "Disease Stage", rep(c("", "1", "2", "3"), 3))
)
flextable(cancerCountWide, col_keys = my_header$col_keys) %>%
set_header_df(
mapping = my_header,
key = "col_keys"
) %>%
theme_booktabs() %>%
merge_v(part = "header") %>%
merge_h(part = "header") %>%
align(align = "center", part = "all") %>%
autofit() %>%
empty_blanks() %>%
fix_border_issues()
确保my_header
包含所有col_keys
描述并使用col_keys
参数来获取空白列(如果col_keys
原始数据集中不存在其中之一,则它是假/空白列)。
> my_header
col_keys line2 line3
1 time Follow-up Follow-up
2 histo Histology Disease Stage
3 blank1
4 1_1 I 1
5 2_1 I 2
6 3_1 I 3
7 blank2
8 1_2 II 1
9 2_2 II 2
10 3_2 II 3
11 blank3
12 1_3 III 1
13 2_3 III 2
14 3_3 III 3
你可以在这里找到文档:https ://davidgohel.github.io/flextable/
推荐阅读
- python - 如何用括号固定 pipenv 要求?
- java - 如何将 PDF 元素与 Java 合并
- .net - .NET Core 项目是否存在 MSBuild 错误,即项目依赖项中的 DLL 引用未复制到 bin 中?
- mysql - 如何在 MYSQL 中比较这些日期?
- javascript - 带有浏览器控制台和 iframe 的 document.onkeypress
- angular - DivIcon 内的 Leaflet Angular 组件
- javascript - json 的目标元素在更改时字符串化为“闪烁”
- php - 是否可以通过注释添加外键而不在 Doctrine 中添加关联?
- flutter - 使用 NeverScrollableScrollPhysics 颤动滚动 GridView
- php - Wordpress PHP 包装一些文本,而不是其他?