R:(可格式化)有没有办法让 color_bar 有条件?
R: (Formattable) Is there a way to make color_bar with condition?
我正在尝试将绿色条中的“是”变为红色条中的“否”..但是使用下面的代码只能将单词变成green/red..我尝试做x ~ formattable::style(color_bar = ifelse(x == "yes", "green"
但它不是那样工作的..
YesNo = formattable::color_bar("red")
使用这段代码我得到了这个输出..但我试图让绿色的“是”和红色的“否”..
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 <- formattable::formatter("span",
style = x ~ formattable::style(color = ifelse(x == "yes", "green",
ifelse(x == "no", "red", "green"))))
我们可以在 background-color
中创建条件并在 formattable
中传递该条件
color_formatter <- formattable::formatter(
"span",
style = x ~ style(
color = 'white',
'background-color' =
ifelse(x == "yes", "green", "red")
))
formattable::formattable(df, list(
YesNo = color_formatter,
Numbers = formattable::color_tile("transparent","lightgreen")))
-输出
我正在尝试将绿色条中的“是”变为红色条中的“否”..但是使用下面的代码只能将单词变成green/red..我尝试做x ~ formattable::style(color_bar = ifelse(x == "yes", "green"
但它不是那样工作的..
YesNo = formattable::color_bar("red")
使用这段代码我得到了这个输出..但我试图让绿色的“是”和红色的“否”..
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 <- formattable::formatter("span",
style = x ~ formattable::style(color = ifelse(x == "yes", "green",
ifelse(x == "no", "red", "green"))))
我们可以在 background-color
中创建条件并在 formattable
color_formatter <- formattable::formatter(
"span",
style = x ~ style(
color = 'white',
'background-color' =
ifelse(x == "yes", "green", "red")
))
formattable::formattable(df, list(
YesNo = color_formatter,
Numbers = formattable::color_tile("transparent","lightgreen")))
-输出