You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R的flextable中实现Results列的复杂条件着色?

问题描述

现有R中的tibble数据框table_factors,需转换为flextable并对Results列按以下复杂条件实现部分文本着色:

  • 第1行:若source[1]处于18.5 ≤ PIR <25范围,仅数字部分设为绿色,否则设为红色(其余文本保持黑色);
  • 第2行:若source[2]小于10,仅数字部分设为绿色,否则设为红色(其余文本保持黑色);
  • 第3行:若数字部分小于55则设为红色,否则设为绿色(其余文本保持黑色);
  • 第4行:无论source[4]取值,仅数字部分设为红色;
  • 第5行:若source[5]小于50,将数字及“of those above 30 ”部分设为红色,否则全部文本设为绿色。

附现有代码:

source = c(
  pir_report = 22,
  users_per = 33,
  usage_report = 75,
  r30 = 40,
  pyth35  = 75
)

table_factors = tibble(
  Factor = c("A", "B", "C", "D", "E"),
  Target = c(
    "18.5 ≤ PIR < 25",
    "Users less than 10%",
    "Minimum 150 min/week",
    "R users age",
    "Python users age"),
  Results = c(
    paste(source[1], "% Within normal range"),
    paste(source[2], "% Are R users"),
    paste(source[3], "% use R 40 hours/week or more"),
    paste(source[4], "% of those above 30 years old use R."),
    paste(source[5], "% of those above 30 use Python and C++"))) %>%
  dplyr::rename("Results" = "Results",
                "Target" = "Target",
                "Factor" = "Factor")

table_factors %>%
  flextable::flextable() %>%
  flextable::width(j = 1, width = 1) %>%
  flextable::width(j = 2, width = 3) %>%
  flextable::width(j = 3, width = 7) %>%
  flextable::bg(i = 1,
                j = 1:ncol(table_factors),
                bg = "#0070C0",
                part = "header") %>%
  flextable::color(i = 1,
                   j = 1:ncol(table_factors),
                   color = "white",
                   part = "header") %>%
  flextable::bg(i = 1:nrow(table_factors),
                j = 1,
                bg = "#0070C0",
                part = "body") %>%
  flextable::color(i = 1:nrow(table_factors),
                   j = 1,
                   color = "white",
                   part = "body") %>%
  flextable::bg(i = seq(2, nrow(table_factors), by = 2),
                j = 2:ncol(table_factors),
                bg = "#F2F2F2",
                part = "body") %>%
  flextable::bg(i = seq(1, nrow(table_factors), by = 2),
                j = 2:ncol(table_factors),
                bg = "#E6E6FA",
                part = "body")
解决方案

要实现Results列的部分文本条件着色,核心是利用flextable的compose()函数结合as_paragraph()构建带样式的文本段落,针对每行的不同条件分别处理。以下是整合所有着色逻辑的完整代码:

library(tibble)
library(flextable)
library(dplyr)

source = c(
  pir_report = 22,
  users_per = 33,
  usage_report = 75,
  r30 = 40,
  pyth35  = 75
)

table_factors = tibble(
  Factor = c("A", "B", "C", "D", "E"),
  Target = c(
    "18.5 ≤ PIR < 25",
    "Users less than 10%",
    "Minimum 150 min/week",
    "R users age",
    "Python users age"),
  Results = c(
    paste(source[1], "% Within normal range"),
    paste(source[2], "% Are R users"),
    paste(source[3], "% use R 40 hours/week or more"),
    paste(source[4], "% of those above 30 years old use R."),
    paste(source[5], "% of those above 30 use Python and C++")))

table_factors %>%
  flextable() %>%
  width(j = 1, width = 1) %>%
  width(j = 2, width = 3) %>%
  width(j = 3, width = 7) %>%
  bg(i = 1, j = 1:ncol(table_factors), bg = "#0070C0", part = "header") %>%
  color(i = 1, j = 1:ncol(table_factors), color = "white", part = "header") %>%
  bg(i = 1:nrow(table_factors), j = 1, bg = "#0070C0", part = "body") %>%
  color(i = 1:nrow(table_factors), j = 1, color = "white", part = "body") %>%
  bg(i = seq(2, nrow(table_factors), by = 2), j = 2:ncol(table_factors), bg = "#F2F2F2", part = "body") %>%
  bg(i = seq(1, nrow(table_factors), by = 2), j = 2:ncol(table_factors), bg = "#E6E6FA", part = "body") %>%
  # 第1行着色逻辑
  compose(
    i = 1, j = "Results",
    value = as_paragraph(
      color(as.character(source[1]), ifelse(source[1] >=18.5 & source[1] <25, "green", "red")),
      " % Within normal range"
    )
  ) %>%
  # 第2行着色逻辑
  compose(
    i = 2, j = "Results",
    value = as_paragraph(
      color(as.character(source[2]), ifelse(source[2] <10, "green", "red")),
      " % Are R users"
    )
  ) %>%
  # 第3行着色逻辑
  compose(
    i = 3, j = "Results",
    value = as_paragraph(
      color(as.character(source[3]), ifelse(source[3] <55, "red", "green")),
      " % use R 40 hours/week or more"
    )
  ) %>%
  # 第4行着色逻辑
  compose(
    i = 4, j = "Results",
    value = as_paragraph(
      color(as.character(source[4]), "red"),
      " % of those above 30 years old use R."
    )
  ) %>%
  # 第5行着色逻辑
  compose(
    i = 5, j = "Results",
    value = as_paragraph(
      if(source[5] <50){
        as_paragraph(
          color(as.character(source[5]), "red"),
          color(" % of those above 30 ", "red"),
          "use Python and C++"
        )
      } else {
        color(paste(source[5], "% of those above 30 use Python and C++"), "green")
      }
    )
  )

关键说明

  • compose()用于指定要修改的行和列,as_paragraph()将不同样式的文本片段组合成完整内容;
  • color()函数配合ifelse()实现条件式着色,针对文本片段单独设置颜色;
  • 第5行通过嵌套as_paragraph()实现多段文本的精准着色,满足复杂的范围着色需求。

内容的提问来源于stack exchange,提问作者Homer Jay Simpson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 14:17:13