flextable 中是否有一个函数可以将 table 中的几行分组到一个标签下?
Is there a function in flextable to group a few rows in a table together under a label?
我想在 flextable 中生成一个 table 将某些行组合在一起
例如使用数据:
df<-structure(list(` ` = c("Group", "Age",
"Residence", "Smoker", "Europe"
), `1` = c("63", "25 ", "25",
"15", "15"), `2` = c("23",
"53 ", "53", "74", "11"),
`3` = c("85", "22", "43",
"13", "15")), row.names = c(NA, -5L), class = c("tbl_df",
"tbl", "data.frame"))
制作table
df<-flextable(df) %>%
add_footer_lines("Observed event") %>%
color(part = "footer", color = "#800000") %>%
bold( bold = TRUE,part="header") %>%
width(j = NULL, width = 1, unit = "in") %>%
autofit()
我想在 smoker 和 Europe 行上方添加一个分组行,称为 'demographics'。使用更长的 tables 这些分组使其更易于阅读。 kable(group_rows
或 pack_rows
)中有类似的东西,但我还没有找到 flextable。
也许你可以尝试这样的事情:
library(flextable)
# to manipulate data
library(dplyr)
df %>%
# adding a grouping variable
mutate(grouping_var = c('','','','demographics','demographic')) %>%
# define as grouped data
as_grouped_data( groups = c("grouping_var"), columns = NULL) %>%
# equal to your code
flextable() %>%
add_footer_lines("Observed event") %>%
color(part = "footer", color = "#800000") %>%
bold( bold = TRUE,part="header") %>%
width(j = NULL, width = 1, unit = "in") %>%
autofit()
另一个可能的解决方案可能是使用 ftExtra
库(它扩展了 flextable
库的功能)。所以,请在下面找到一个代表。
Reprex
- 建议代码
library(flextable)
library(ftExtra)
library(dplyr)
grouped_df <- df %>%
mutate(category = c("", "", "", "demographics", "demographics")) %>%
group_by(category) %>%
as_flextable(hide_grouplabel = TRUE) %>%
bold(j = 1, i = ~ !is.na(category), bold = TRUE, part = "body" ) %>%
add_footer_lines("Observed event") %>%
color(part = "footer", color = "#800000") %>%
bold(bold = TRUE, part = "header") %>%
width(j = NULL, width = 1, unit = "in") %>%
autofit()
- 输出
grouped_df
由 reprex package (v2.0.1)
于 2022-03-29 创建
有一个函数可以构造一个分组 data.frame,其中分组打印为行分隔符。所以这个答案只适用于你有一个在 data.frame 中可用的组。我认为结果与 'ftExtra' 提供的结果相同,只是数据准备步骤不同。
我在示例中添加了一个名为 type
的列用于此角色。
library(flextable)
library(magrittr)
df <- structure(list(
type = c("glop glop", "glop glop" , "glop glop", "pas glop pas glop", "pas glop pas glop"),
what = c("Group", "Age", "Residence", "Smoker", "Europe"),
`1` = c(63, 25, 25, 15, 15),
`2` = c(23, 53, 53, 74, 11),
`3` = c(85, 22, 43, 13, 15)
),
row.names = c(NA, -5L),
class = c("data.frame"))
df
#> type what 1 2 3
#> 1 glop glop Group 63 23 85
#> 2 glop glop Age 25 53 22
#> 3 glop glop Residence 25 53 43
#> 4 pas glop pas glop Smoker 15 74 13
#> 5 pas glop pas glop Europe 15 11 15
一些默认设置:
set_flextable_defaults(font.color = "#333333", border.color = "#999999", padding = 4)
现在是 flextable。首先 as_grouped_data()
重构 data.frame,然后 as_flextable()
轻松将其转换为 flextable。
as_grouped_data(df, groups = "type") %>%
as_flextable() %>%
add_footer_lines("Observed event") %>%
set_header_labels(what = "") %>%
color(part = "footer", color = "#800000") %>%
bold( bold = TRUE, part="header") %>%
align(i = ~ !is.na(type), align = "center") %>%
bold(i = ~ !is.na(type))
我想在 flextable 中生成一个 table 将某些行组合在一起
例如使用数据:
df<-structure(list(` ` = c("Group", "Age",
"Residence", "Smoker", "Europe"
), `1` = c("63", "25 ", "25",
"15", "15"), `2` = c("23",
"53 ", "53", "74", "11"),
`3` = c("85", "22", "43",
"13", "15")), row.names = c(NA, -5L), class = c("tbl_df",
"tbl", "data.frame"))
制作table
df<-flextable(df) %>%
add_footer_lines("Observed event") %>%
color(part = "footer", color = "#800000") %>%
bold( bold = TRUE,part="header") %>%
width(j = NULL, width = 1, unit = "in") %>%
autofit()
我想在 smoker 和 Europe 行上方添加一个分组行,称为 'demographics'。使用更长的 tables 这些分组使其更易于阅读。 kable(group_rows
或 pack_rows
)中有类似的东西,但我还没有找到 flextable。
也许你可以尝试这样的事情:
library(flextable)
# to manipulate data
library(dplyr)
df %>%
# adding a grouping variable
mutate(grouping_var = c('','','','demographics','demographic')) %>%
# define as grouped data
as_grouped_data( groups = c("grouping_var"), columns = NULL) %>%
# equal to your code
flextable() %>%
add_footer_lines("Observed event") %>%
color(part = "footer", color = "#800000") %>%
bold( bold = TRUE,part="header") %>%
width(j = NULL, width = 1, unit = "in") %>%
autofit()
另一个可能的解决方案可能是使用 ftExtra
库(它扩展了 flextable
库的功能)。所以,请在下面找到一个代表。
Reprex
- 建议代码
library(flextable)
library(ftExtra)
library(dplyr)
grouped_df <- df %>%
mutate(category = c("", "", "", "demographics", "demographics")) %>%
group_by(category) %>%
as_flextable(hide_grouplabel = TRUE) %>%
bold(j = 1, i = ~ !is.na(category), bold = TRUE, part = "body" ) %>%
add_footer_lines("Observed event") %>%
color(part = "footer", color = "#800000") %>%
bold(bold = TRUE, part = "header") %>%
width(j = NULL, width = 1, unit = "in") %>%
autofit()
- 输出
grouped_df
由 reprex package (v2.0.1)
于 2022-03-29 创建有一个函数可以构造一个分组 data.frame,其中分组打印为行分隔符。所以这个答案只适用于你有一个在 data.frame 中可用的组。我认为结果与 'ftExtra' 提供的结果相同,只是数据准备步骤不同。
我在示例中添加了一个名为 type
的列用于此角色。
library(flextable)
library(magrittr)
df <- structure(list(
type = c("glop glop", "glop glop" , "glop glop", "pas glop pas glop", "pas glop pas glop"),
what = c("Group", "Age", "Residence", "Smoker", "Europe"),
`1` = c(63, 25, 25, 15, 15),
`2` = c(23, 53, 53, 74, 11),
`3` = c(85, 22, 43, 13, 15)
),
row.names = c(NA, -5L),
class = c("data.frame"))
df
#> type what 1 2 3
#> 1 glop glop Group 63 23 85
#> 2 glop glop Age 25 53 22
#> 3 glop glop Residence 25 53 43
#> 4 pas glop pas glop Smoker 15 74 13
#> 5 pas glop pas glop Europe 15 11 15
一些默认设置:
set_flextable_defaults(font.color = "#333333", border.color = "#999999", padding = 4)
现在是 flextable。首先 as_grouped_data()
重构 data.frame,然后 as_flextable()
轻松将其转换为 flextable。
as_grouped_data(df, groups = "type") %>%
as_flextable() %>%
add_footer_lines("Observed event") %>%
set_header_labels(what = "") %>%
color(part = "footer", color = "#800000") %>%
bold( bold = TRUE, part="header") %>%
align(i = ~ !is.na(type), align = "center") %>%
bold(i = ~ !is.na(type))