首页 > 解决方案 > 使用 flextable 在表格的不同部分之间共享脚注

问题描述

我需要创建表,在表的标题和正文中都放置了相同的脚注,我无法弄清楚如何使用它来实现flextable,我可以创建的内容如下:

library(flextable)
library(dplyr)
library(tidyr)

data(iris)
iris %>%
  as_tibble %>%
  gather(.,key = variable,value = value,-Species) %>%
  group_by(Species,variable) %>%
  summarise(value=formatC(mean(value),digits = 2,format = 'f')) %>%
  ungroup %>%
  spread(.,key = variable,value = value) %>%
  flextable %>%
  footnote(.,part = 'header',i = 1,j = c(2:5),
           value = as_paragraph(c('Rounded to two decimal places')),
           ref_symbols = c('*'),
           inline=FALSE) %>%
  footnote(.,part = 'body',i = c(1:3),j = 1,
           value = as_paragraph(c('Rounded to two decimal places')),
           ref_symbols = c('*'),
           inline=FALSE)

目前我为页眉和正文创建了两个具有相同语句的脚注,我想知道是否可以将这两个语句合并为一个。

谢谢!

标签: rflextablefootnotes

解决方案


(我没想到在实现此功能时会重复脚注,但是)使用merge_v,如果相同,您可以合并它们:

library(flextable)
library(dplyr)
library(tidyr)

data(iris)
iris %>%
  as_tibble %>%
  gather(.,key = variable,value = value,-Species) %>%
  group_by(Species,variable) %>%
  summarise(value=formatC(mean(value),digits = 2,format = 'f')) %>%
  ungroup %>%
  spread(.,key = variable,value = value) %>%
  flextable %>%
  footnote(.,part = 'header',i = 1,j = c(2:5),
           value = as_paragraph(c('Rounded to two decimal places')),
           ref_symbols = c('*'),
           inline=FALSE) %>%
  footnote(.,part = 'body',i = c(1:3),j = 1,
           value = as_paragraph(c('Rounded to two decimal places')),
           ref_symbols = c('*'),
           inline=FALSE) %>% 
  merge_v(part = "footer")

在此处输入图像描述


推荐阅读