flextable 中有条件的粗体值

conditionally bold values in flextable

是否可以bold/color根据 tstat 估计值。

对于 ex - 如果 tstat 大于 1.96 则粗体估计值

这是 的延续,我必须使用 flextable。

library(dplyr)
library(flextable)


attribute <- c("b0", "b1", "b2", "b3", "b4", "b5")
estimate <- round(runif(n = 6, min = 0, max = 5), 2)
tstat <- round(runif(n = 6, min = 0, max = 5), 2)

# tibble
tbl <- tibble(attribute, estimate, tstat) %>%
  as_flextable()

我们可以使用 ifelsecase_whenbolding

添加 **<value>**
library(dplyr)
library(flextable)
tbl <- tibble( attribute, estimate, tstat) %>%
       mutate(estimate = case_when(tstat > 1.96 
            ~sprintf('**%0.2f**', estimate), 
          TRUE  ~sprintf('%0.2f', estimate))) %>%
  as_flextable() %>%
  colformat_md()
tbl

-输出

另一个解决方案:

library(dplyr)
library(flextable)

set.seed(4123)

attribute <- c("b0", "b1", "b2", "b3", "b4", "b5")
estimate <- round(runif(n = 6, min = 0, max = 5), 2)
tstat <- round(runif(n = 6, min = 0, max = 5), 2)

tbl <- tibble(attribute, estimate, tstat)
  
tbl %>%   
  flextable() %>% 
  bold(tbl$tstat > 1.96,2)

另一种使用 flextable 行选择器的解决方案:

library(dplyr)
library(flextable)

set.seed(4123)

attribute <- c("b0", "b1", "b2", "b3", "b4", "b5")
estimate <- round(runif(n = 6, min = 0, max = 5), 2)
tstat <- round(runif(n = 6, min = 0, max = 5), 2)

tbl <- tibble(attribute, estimate, tstat)

tbl %>%   
  flextable() %>% 
  bold(~ tstat > 1.96,2)