简体   繁体   English

如何在R中的flextable中灵活添加标题行名称?

[英]How to flexibly add header row names in flextable in R?

I'm trying to make a function that will automatically generate a flextable depending on users input, and I want the headers to be as non-repetitive as possible.我正在尝试制作一个根据用户输入自动生成灵活表的功能,并且我希望标题尽可能不重复。 For example, the current column names are:例如,当前列名是:

raw_names <- c("name", "V1_avg", "V1_median", "V2_avg", "V2_median", "V3_avg", "V3_median")

For this table, I want the top header to have the variable name (V1, V2, V3) and the sub-header to be Avg and Med underneath the appropriate variable.对于此表,我希望顶部标题具有变量名称(V1、V2、V3),而子标题在相应变量下方为 Avg 和 Med。 There will be nothing over name in the top header (see below code)顶部标题中不会有任何名称(请参见下面的代码)

I know how to do this if I were to explicitly state that manually, but I want this function to be flexible because sometimes the dataset may have V4 or V5.如果我要手动明确说明,我知道如何执行此操作,但我希望此功能灵活,因为有时数据集可能具有 V4 或 V5。 The input will always look like the above, but it may have varying numbers of variables.输入将始终如上所示,但它可能具有不同数量的变量。

What's tripping me up is that for these main headers, you need to specify the length of the header rows, but I'm not sure how many variables there will be:让我感到困惑的是,对于这些主要标题,您需要指定标题行的长度,但我不确定会有多少变量:

library(flextable)
    add_header_row(colwidths = c(1, 2, 2, 2),
                   values = c(NA_character_, "V1", "V2", "V3"))

Any thoughts?有什么想法吗?

If your raw_names will always follow the same pattern, perhaps something like this to prepare add_header_row() arguments:如果您的raw_names将始终遵循相同的模式,也许像这样准备add_header_row()参数:

col_widths_vect <- function(names_vect) c(1, rep(2, (length(names_vect) - 1) / 2))

col_vals_vect <- function(names_vect) {
  return(
    c(
      NA_character_,
      names_vect[2:length(names_vect)] |>
        sub("_.*", "", x = _) |>
        unique()
    )
  )
}

raw_names <- c("name", "V1_avg", "V1_median", "V2_avg", "V2_median", "V3_avg", "V3_median")
col_widths_vect(raw_names)
#> [1] 1 2 2 2
col_vals_vect(raw_names)
#> [1] NA   "V1" "V2" "V3"

I have created a function (only available on github for now) named separate_header .我创建了一个名为separate_header的函数(目前仅在 github 上可用)。 It uses data.table::tstrsplit() that is doing exactly what you were trying to do.它使用data.table::tstrsplit()来做你想做的事情。

x <- data.frame(
  Species = as.factor(c("setosa", "versicolor", "virginica")),
  Sepal.Length_mean = c(5.006, 5.936, 6.588),
  Sepal.Length_sd = c(0.35249, 0.51617, 0.63588),
  Sepal.Width_mean = c(3.428, 2.77, 2.974),
  Sepal.Width_sd = c(0.37906, 0.3138, 0.3225),
  Petal.Length_mean = c(1.462, 4.26, 5.552),
  Petal.Length_sd = c(0.17366, 0.46991, 0.55189),
  Petal.Width_mean = c(0.246, 1.326, 2.026),
  Petal.Width_sd = c(0.10539, 0.19775, 0.27465)
)

ft_1 <- flextable(x)
ft_1 <- colformat_double(ft_1, digits = 2)
ft_1 <- theme_box(ft_1)
ft_1 <- separate_header(
  x = ft_1,
  opts = c("span-top", "bottom-vspan")
)
ft_1

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM