是否可以在标签下的桌子中将几行排成几行的功能?

发布于 2025-01-17 13:24:20 字数 912 浏览 4 评论 0原文

我想在 flextable 中生成一个表,将某些行分组在一起

例如使用数据:

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"))

制作表

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() 

我想在吸烟者和欧洲行上方添加一个分组行,称为“人口统计”。对于较长的表格,这些分组使其更易于阅读。 kable 中有类似的东西(group_rowspack_rows),但我还没有找到适用于 flextable 的东西。

图像当前表

I would like to produce a table in flextable that groups together certain rows

For example using the data:

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"))

Make 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() 

I would like to add a grouping row above the smoker and Europe rows, called 'demographics'. With longer tables these groupings make it easier to read. There is something similar in kable (group_rows or pack_rows) but I haven't found one for flextable.

image of current table

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倾其所爱 2025-01-24 13:24:20

有一个函数可以构造分组的 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)

现在是弹性表。首先使用 as_grouped_data() 重构 data.frame,然后使用 as_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))

输入图片此处描述

There is a function that structure a grouped data.frame where groups are printed as rows separators. So this answer only applies if you have a group that is available in the data.frame. I think the result is the same than the one provided with 'ftExtra', it only differs in the data preparation step.

I added to the example a column named type to be used for this role.

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

Few default settings:

set_flextable_defaults(font.color = "#333333", border.color = "#999999", padding = 4)

And now the flextable. First as_grouped_data() to restructure the data.frame, then as_flextable() to turn it as a flextable easily.

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))

enter image description here

彩扇题诗 2025-01-24 13:24:20

也许您可以尝试这样的尝试:

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()

”在此处输入图像说明”

Maybe you can try something like this:

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()

enter image description here

哽咽笑 2025-01-24 13:24:20

另一个可能的解决方案可能是使用ftextra库(它扩展了Flextable库的功能)。因此,请在下面找到Reprex。

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

”在此处输入图像描述

在2022-03-29上创建的 reprex软件包(v2.0.1)

Another possible solution might be to use the ftExtra library (which extends the functions of the flextable library). So, please find below a reprex.

Reprex

  • Suggested code
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()  
  • Output
grouped_df

enter image description here

Created on 2022-03-29 by the reprex package (v2.0.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文