gpt4 book ai didi

javascript - 使用 DT 进行列格式化的单个/多个条件

转载 作者:行者123 更新时间:2023-12-03 01:24:53 24 4
gpt4 key购买 nike

这与Is there any similar approach to conditional formating for multiple columns from excel in Shiny中的问题有关,,提供的解决方案工作正常,但我对如何扩展代码以满足新的要求有点了解。因此,如果我有以下数据框并希望根据以下条件更改五列的背景颜色:

  • 对于 X、Y 列
  • 如果 -4 < X < 4 且 Y < 10 X,Y 的颜色为粉色
  • elseif Y >X,Y 的 10 种颜色为蓝色
  • 否则 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]);
}"

提前谢谢

最佳答案

开始

datatable(df, escape=FALSE, 
options = list(rowCallback=JS(jscode)))

并添加formatStyle

library(DT)
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),
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)
)

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))) %>%
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'))
)

enter image description here

关于javascript - 使用 DT 进行列格式化的单个/多个条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51594858/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com