R 在对数据表进行排序/筛选时,如何确保行着色更新?
我正在写一个R闪亮的应用程序。我使用带有特定颜色的at DT datatable作为文本条目。重新调整表格时,颜色与正确的行不一致。相反,它们留在原地 我假设我需要观察表被重新排序/过滤的事件并对其作出反应。我该怎么做 下面是示例代码R 在对数据表进行排序/筛选时,如何确保行着色更新?,r,shiny,dt,R,Shiny,Dt,我正在写一个R闪亮的应用程序。我使用带有特定颜色的at DT datatable作为文本条目。重新调整表格时,颜色与正确的行不一致。相反,它们留在原地 我假设我需要观察表被重新排序/过滤的事件并对其作出反应。我该怎么做 下面是示例代码 library(shiny) library(DT) ui= shinyUI(fluidPage( titlePanel("Test reorder DT datatave"), sidebarLayout( actionButton("butt
library(shiny)
library(DT)
ui= shinyUI(fluidPage(
titlePanel("Test reorder DT datatave"),
sidebarLayout(
actionButton("button does nothing", "nothing")
,
mainPanel(
DT::dataTableOutput("mydt")
)
)
))
server <- function(input, output) {
output$mydt <- DT::renderDataTable({
dat <- data.frame(src=c(1,2,3), tgt=c("█", "█", "█"))
mycolors <- c("dodgerblue2", "grey", "firebrick2")
rgbcolors <- apply(grDevices::col2rgb(mycolors), 2,
function(rgb) sprintf("rgb(%s)", paste(rgb, collapse=",")))
column <- 2
jscode <- paste("function(row, data, index) {",
sprintf("var colors=%s;\n$(this.api().cell(index,
%s).node()).css('color', colors[index]);",
sprintf("[%s]", paste(sprintf("'%s'", rgbcolors),
collapse=", ")), column), "}", sep="\n")
datatable(dat, escape=FALSE,
options = list(rowCallback=JS(jscode))
)
})
}
shinyApp(ui = ui, server = server)
库(闪亮)
图书馆(DT)
ui=shinyUI(fluidPage(
titlePanel(“测试重新排序DT数据”),
侧边栏布局(
actionButton(“按钮不做任何事情”,“什么也不做”)
,
主面板(
DT::dataTableOutput(“mydt”)
)
)
))
服务器可以直接在tgt
列中设置颜色吗?像这样:
mycolors <- c("dodgerblue2", "grey", "firebrick2")
rgbcolors <- apply(grDevices::col2rgb(mycolors), 2,
function(rgb) sprintf("rgb(%s)", paste(rgb, collapse=",")))
tgt <- sprintf('<span style="color:%s">█</span>', rgbcolors)
dat <- data.frame(src=c(1,2,3), tgt=tgt)
datatable(dat, escape=FALSE)
mycolors不用回调,可以直接在tgt
列中设置颜色。会不会有问题?太好了。非常感谢。