这是关系到这个问题是否有任何类似的方法来有条件格式化为从Excel中闪亮的多列 ,,提供的解决方案正常工作,但我在如何扩展代码来满足新的要求有点堆栈。 所以,如果我有以下的数据帧,并想改变基于以下条件五列的背景色:
- 列的X,Y
- 如果-4 <X <4和Y <X的10色中,Y是粉红色
- ELSEIF Y X> 10颜色,Y是布鲁
否则X =“”或Y =“”,那么X的颜色,Y是白色
用于列A,B,C
- 如果“A” <3,那么“A”是绿色的粉红色的其他
- 如果“B” <3,则“B”为绿色粉别的
- 如果“C” <3,那么“C”是绿色的粉红色的其他
我想在这里延伸提供了解决方案,但没有奏效。 有没有人帮助如何办理这个。
output$contents <- renderDataTable({
df <- data.frame(
id = 1:10,
X = c(-2, 4, 40, -0.1228, 2.9, 9, 2.7, 2.7, 31, -30),
Y = c(-18.9, -19.5, 19.6, 12, 11.1, 73, 4.3, 39, 2.5, 1.6),
A = c(-7.3, 5.1 ,0.12, 15, 21, 1.2, -0,07, 4.3, 39, 2.5)
B = c(-18.9, 0.12, 15, 11.1, 73, -2, 4, 40, -19.5, 19.6)
C = c(4.3, 39, 2.5, 1.6, -7.3, 6, 5.1 ,0.12, -0.07, 4.3)
library(DT)
datatable(df) %>% formatStyle(
'A',
target = 'cell',
backgroundColor = styleInterval(3, c('green','pink')))
%>% formatStyle(
'B',
target = 'cell',
backgroundColor = styleInterval(3, c('green','pink'))
)%>% formatStyle(
'C',
target = 'cell',
backgroundColor = styleInterval(3, c('green','pink'))
)
colors <- with(df, ifelse(X > -4 & X < 4 & Y < 10,
"pink",
ifelse(Y > 10,
"blue", "white")))
rgbcolors <- apply(grDevices::col2rgb(colors), 2,
function(rgb) sprintf("rgb(%s)", paste(rgb, collapse=",")))
columns <- c(2,3) # columns X and Y
jscode <-
paste("function(row, data, index) {",
sprintf("var colors=%s;\n%s",
sprintf("[%s]",
paste(sprintf("'%s'", rgbcolors), collapse=", ")),
paste(sprintf("$(this.api().cell(index, %s).node()).css('background-color', colors[index]);",
columns), collapse="\n")),
"}", sep="\n")
datatable(df, escape=FALSE,
options = list(rowCallback=JS(jscode))
)
jscode <- "function(row, data, index) {
var colors = ['rgb(255,192,203)', 'rgb(255,255,255)', 'rgb(0,0,255)', 'rgb(0,0,255)', 'rgb(0,0,255)', 'rgb(0,0,255)', 'rgb(255,192,203)', 'rgb(0,0,255)', 'rgb(255,255,255)', 'rgb(255,255,255)'];
$(this.api().cell(index, 2).node()).css('background-color', colors[index]);
$(this.api().cell(index, 3).node()).css('background-color', colors[index]);
}"
先感谢您