使用 flextable 分类并突出显示 table 个部分
Categorize and highlight table sections with flextable
我有以下按类别
列分类的数据框
> dput(mydata)
structure(list(Category = c("Executive", "Management", "Management",
"Management", "Professional", "Professional", "Professional",
"Para-Professional", "Para-Professional", "Para-Professional"
), Rank = c("Rank 1", "Rank 1", "Rank 2", "Rank 3", "Rank 1",
"Rank 2", "Rank 3", "Rank 1", "Rank 2", "Rank 3"), Jobs = c(" SMP - Sales, Marketing & Product Management",
" SMP - Sales, Marketing & Product Management", " ENS - Engineering & Science",
" FIN - Finance", " SMP - Sales, Marketing & Product Management",
" ENS - Engineering & Science", " FIN - Finance", " PSK - Production & Skilled Trades",
" ENS - Engineering & Science", " EGS - Energy Generation & Supply"
), N = c(3, 10, 3, 2, 54, 25, 5, 7, 2, 1)), row.names = c(NA,
-10L), class = "data.frame")
我想为类别列合并相同类别的单元格,使用 flex table
mydata%>% flextable()%>% merge_v(j=~Category)
现在我想突出显示与每个类别对应的数据的整个部分边框,例如,对应于执行类别的数据应该用更宽的边框突出显示,对于其他类似。我试过并且只能突出显示如下的类别单元格:
cl<-fp_border(color = "#00A8C8",width = 3)
mydata%>% flextable()%>% merge_v(j=~Category)%>%hline(j=~Category,border = cl)
我希望每个类别对应的所有子 table 周围都有类似的边框,以便更好地区分 table 中的类别。仅使用 flextable 怎么办?
我认为这应该有所帮助。
这里的关键是找到一种方法来创建一个逻辑向量来表示线条应该出现的位置 - 这就是函数 break_position
正在做的事情。
library(flextable)
library(officer)
library(magrittr)
mydata <- structure(list(Category = c(
"Executive", "Management", "Management",
"Management", "Professional", "Professional", "Professional",
"Para-Professional", "Para-Professional", "Para-Professional"
), Rank = c(
"Rank 1", "Rank 1", "Rank 2", "Rank 3", "Rank 1",
"Rank 2", "Rank 3", "Rank 1", "Rank 2", "Rank 3"
), Jobs = c(
" SMP - Sales, Marketing & Product Management",
" SMP - Sales, Marketing & Product Management", " ENS - Engineering & Science",
" FIN - Finance", " SMP - Sales, Marketing & Product Management",
" ENS - Engineering & Science", " FIN - Finance", " PSK - Production & Skilled Trades",
" ENS - Engineering & Science", " EGS - Energy Generation & Supply"
), N = c(3, 10, 3, 2, 54, 25, 5, 7, 2, 1)), row.names = c(
NA,
-10L
), class = "data.frame")
cl <- fp_border(color = "#00A8C8", width = 3)
break_position <- function(x) {
z <- data.table::rleidv(x)
c(z[-length(z)] != z[-1], FALSE)
}
mydata %>%
flextable() %>%
merge_v(j = ~Category) %>%
hline(i = ~ break_position(Category), border = cl) %>%
fix_border_issues()
我有以下按类别
列分类的数据框> dput(mydata)
structure(list(Category = c("Executive", "Management", "Management",
"Management", "Professional", "Professional", "Professional",
"Para-Professional", "Para-Professional", "Para-Professional"
), Rank = c("Rank 1", "Rank 1", "Rank 2", "Rank 3", "Rank 1",
"Rank 2", "Rank 3", "Rank 1", "Rank 2", "Rank 3"), Jobs = c(" SMP - Sales, Marketing & Product Management",
" SMP - Sales, Marketing & Product Management", " ENS - Engineering & Science",
" FIN - Finance", " SMP - Sales, Marketing & Product Management",
" ENS - Engineering & Science", " FIN - Finance", " PSK - Production & Skilled Trades",
" ENS - Engineering & Science", " EGS - Energy Generation & Supply"
), N = c(3, 10, 3, 2, 54, 25, 5, 7, 2, 1)), row.names = c(NA,
-10L), class = "data.frame")
我想为类别列合并相同类别的单元格,使用 flex table
mydata%>% flextable()%>% merge_v(j=~Category)
现在我想突出显示与每个类别对应的数据的整个部分边框,例如,对应于执行类别的数据应该用更宽的边框突出显示,对于其他类似。我试过并且只能突出显示如下的类别单元格:
cl<-fp_border(color = "#00A8C8",width = 3)
mydata%>% flextable()%>% merge_v(j=~Category)%>%hline(j=~Category,border = cl)
我希望每个类别对应的所有子 table 周围都有类似的边框,以便更好地区分 table 中的类别。仅使用 flextable 怎么办?
我认为这应该有所帮助。
这里的关键是找到一种方法来创建一个逻辑向量来表示线条应该出现的位置 - 这就是函数 break_position
正在做的事情。
library(flextable)
library(officer)
library(magrittr)
mydata <- structure(list(Category = c(
"Executive", "Management", "Management",
"Management", "Professional", "Professional", "Professional",
"Para-Professional", "Para-Professional", "Para-Professional"
), Rank = c(
"Rank 1", "Rank 1", "Rank 2", "Rank 3", "Rank 1",
"Rank 2", "Rank 3", "Rank 1", "Rank 2", "Rank 3"
), Jobs = c(
" SMP - Sales, Marketing & Product Management",
" SMP - Sales, Marketing & Product Management", " ENS - Engineering & Science",
" FIN - Finance", " SMP - Sales, Marketing & Product Management",
" ENS - Engineering & Science", " FIN - Finance", " PSK - Production & Skilled Trades",
" ENS - Engineering & Science", " EGS - Energy Generation & Supply"
), N = c(3, 10, 3, 2, 54, 25, 5, 7, 2, 1)), row.names = c(
NA,
-10L
), class = "data.frame")
cl <- fp_border(color = "#00A8C8", width = 3)
break_position <- function(x) {
z <- data.table::rleidv(x)
c(z[-length(z)] != z[-1], FALSE)
}
mydata %>%
flextable() %>%
merge_v(j = ~Category) %>%
hline(i = ~ break_position(Category), border = cl) %>%
fix_border_issues()