为什么 formattable 会忽略我的条件格式?

Why is formattable ignoring my conditional formatting?

我试图只在“YesNo”列下用“yes”突出显示单词,但它不起作用..我做错了吗?

    df = data.frame(YesNo = c("yes","no","yes","no"), 
                Numbers = c(4, 5, 10, 10))
df

rownames(df) = c("Test1","Test2","Test3","Test4")
df

formattable(df)


sign_formatter <- formatter("span", 
                            style = x ~ style(color = ifelse(x == "yes", "green", 
                                                             ifelse(x =="no", "red", "black"))))

    formattable(df, list(
      YesNo = sign_formatter,
      Numbers = color_tile("transparent","lightgreen")))

问题与代码无关。这是因为 plotly 屏蔽了 formattable 的功能。两者都有很多共同的功能,一旦 plotly 包在 formattable 之后加载,相同的功能将被 plotly 功能屏蔽。补救措施是

  1. 要么在新的 R 会话中执行此操作,只加载 formattable
  2. 使用 :: 表明函数来自那个包

可能问题出在 styleplotly 也有

library(formattable)
sign_formatter <- formattable::formatter("span", 
                            style = x ~ 
      formattable::style(color = ifelse(x == "yes", "green", 
                            ifelse(x =="no", "red", "black"))))

formattable::formattable(df, list(
  YesNo = sign_formatter,
  Numbers = formattable::color_tile("transparent","lightgreen")))

-输出

数据

df = data.frame(YesNo = c("yes","no","yes","no"), 
                Numbers = c(4, 5, 10, 10))
df

rownames(df) = c("Test1","Test2","Test3","Test4")

根据前 'n' 行左右创建条件

n <- 2
sign_formatter <- formatter("span", 
                            style = x ~ style(color = ifelse(seq_along(x) <= n & x == "yes", "green", 
                                                             ifelse(seq_along(x) <= n & x =="no", "red", "black"))))

formattable(df, list(
  YesNo = sign_formatter,
  Numbers = color_tile("transparent","lightgreen")))

-输出