【发布时间】:2017-12-10 15:30:33
【问题描述】:
我正在编写一个 R Shiny 应用程序。我在 DT 数据表中使用特定颜色的文本条目。重新使用表格时,颜色不会保留在正确的行中。相反,它们会留在原地。
我假设我需要观察表被重新排序/过滤的事件并做出反应。我该怎么做?
下面的示例代码。
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)
【问题讨论】:
-
可以直接在
tgt列中设置颜色,而不是使用回调。会不会出问题?