水平组合两个柔性板
Combine horizontally two flextables
假设您获得两个 flextable
objects 并且您意识到需要将它们水平组合的“后验”(即,您需要类似于 rbind
或 bind_rows
,但对于 flextable
objects)。除了标题外,它们具有相同的结构。您知道执行此操作的功能或方法吗?
接下来,我分享一些代码以获得可重现的示例:
library(dplyr)
library(flextable)
# Simulate some data
g_A <- expand.grid(x = "A", y = c("A_1", "A_2"), z = c("A_1_a", "A_1_b", "A_2_a", "A_2_b"))
g_B <- expand.grid(x = "B", y = c("B_1", "B_2"), z = c("B_1_a", "B_1_b", "B_2_a", "B_2_b"))
g <- rbind(g_A, g_B)
n <- 123
set.seed(1)
df <- sample_n(g, n, replace = TRUE)
# Build the table
tmp <- c(table(df$x)[1],
table(df$y)[1],
table(df$z)[1:2],
table(df$y)[2],
table(df$z)[3:4],
table(df$x)[2],
table(df$y)[3],
table(df$z)[5:6],
table(df$y)[4],
table(df$z)[7:8])
my_tab <- data.frame("tmp" = names(tmp), "counts" = tmp, "percentages" = round(tmp/n*100, 2))
# Split the table
my_tab_A <- my_tab[1:7,]
my_tab_B <- my_tab[-(1:7),]
# flextable A
ft <- flextable(my_tab_A)
ft <- set_header_labels(ft, tmp = "")
ft <- align(ft, align = "center")
ft <- align(ft, j = 1, align = "left")
ft <- set_caption(ft, "Table A")
ft_A <- ft
# flextable B
ft <- flextable(my_tab_B)
ft <- set_header_labels(ft, tmp = "")
ft <- align(ft, align = "center")
ft <- align(ft, j = 1, align = "left")
ft <- set_caption(ft, "Table B")
ft_B <- ft
ft_A
:
ft_B
:
预期结果:.
我正在寻找的解决方案不能基于与 my_tab
的合作,因为回想一下,我意识到我想水平组合它们的“后验”。
等待您的见解,
谢谢,
再见
这可能会成功...
ft <- flextable( rbind( ft_A$body$dataset, ft_B$body$dataset ) )
ft <- set_header_labels(ft, tmp = "")
ft <- align(ft, align = "center")
ft <- align(ft, j = 1, align = "left")
ft <- set_caption(ft, "Table 1")
ft_C <- ft
ft_C
假设您获得两个 flextable
objects 并且您意识到需要将它们水平组合的“后验”(即,您需要类似于 rbind
或 bind_rows
,但对于 flextable
objects)。除了标题外,它们具有相同的结构。您知道执行此操作的功能或方法吗?
接下来,我分享一些代码以获得可重现的示例:
library(dplyr)
library(flextable)
# Simulate some data
g_A <- expand.grid(x = "A", y = c("A_1", "A_2"), z = c("A_1_a", "A_1_b", "A_2_a", "A_2_b"))
g_B <- expand.grid(x = "B", y = c("B_1", "B_2"), z = c("B_1_a", "B_1_b", "B_2_a", "B_2_b"))
g <- rbind(g_A, g_B)
n <- 123
set.seed(1)
df <- sample_n(g, n, replace = TRUE)
# Build the table
tmp <- c(table(df$x)[1],
table(df$y)[1],
table(df$z)[1:2],
table(df$y)[2],
table(df$z)[3:4],
table(df$x)[2],
table(df$y)[3],
table(df$z)[5:6],
table(df$y)[4],
table(df$z)[7:8])
my_tab <- data.frame("tmp" = names(tmp), "counts" = tmp, "percentages" = round(tmp/n*100, 2))
# Split the table
my_tab_A <- my_tab[1:7,]
my_tab_B <- my_tab[-(1:7),]
# flextable A
ft <- flextable(my_tab_A)
ft <- set_header_labels(ft, tmp = "")
ft <- align(ft, align = "center")
ft <- align(ft, j = 1, align = "left")
ft <- set_caption(ft, "Table A")
ft_A <- ft
# flextable B
ft <- flextable(my_tab_B)
ft <- set_header_labels(ft, tmp = "")
ft <- align(ft, align = "center")
ft <- align(ft, j = 1, align = "left")
ft <- set_caption(ft, "Table B")
ft_B <- ft
ft_A
:
ft_B
:
预期结果:
我正在寻找的解决方案不能基于与 my_tab
的合作,因为回想一下,我意识到我想水平组合它们的“后验”。
等待您的见解,
谢谢,
再见
这可能会成功...
ft <- flextable( rbind( ft_A$body$dataset, ft_B$body$dataset ) )
ft <- set_header_labels(ft, tmp = "")
ft <- align(ft, align = "center")
ft <- align(ft, j = 1, align = "left")
ft <- set_caption(ft, "Table 1")
ft_C <- ft
ft_C