【问题标题】:Conditional format all flextable cells that match a string条件格式匹配字符串的所有弹性表格单元格
【发布时间】:2019-08-16 03:59:26
【问题描述】:

我有一个在某些单元格中带有字符串“Red”的 flextable。我只想将这些单元格突出显示为红色。

我可以按如下方式逐列检查,但我要使用的实际表中有很多列。这可以在一次调用 bg 或另一个函数中完成吗,类似于 dplyr 中的 mutate_all 函数?

data.frame(A = c("Red", "Other", "Other"), B = c("Green", "Orange", "Red"), stringsAsFactors = F) %>%
  as_tibble() %>%
  flextable() %>%
  bg(i = ~A=='Red', j = ~A, bg = 'red') %>%
  bg(i = ~B=='Red', j = ~B, bg = 'red')

这段代码实现了我的需要,但手动指定了每一列。

【问题讨论】:

    标签: r flextable


    【解决方案1】:

    你对经典方法满意吗?
    如果您没有任何理由在单个管道内执行此操作,我想这是安全的方法。

    library(flextable); library(dplyr)
    
    d <- data.frame(A = c("Red", "Other", "Other"), 
                    B = c("Green", "Orange", "Red"), 
                    stringsAsFactors = F) %>%
      as_tibble()
    
    color_ind <- which(d == "Red", arr.ind = TRUE)
    ft <- flextable(d)
    
    for(i in 1:nrow(color_ind)) {
      ft <- ft %>% 
        bg(i = color_ind[i, 1], j = color_ind[i, 2], bg = 'red')
    }
    
    ft
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      • 2016-08-20
      • 2011-07-01
      • 1970-01-01
      相关资源
      最近更新 更多