Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在对数据表进行排序/筛选时,如何确保行着色更新?_R_Shiny_Dt - Fatal编程技术网

R 在对数据表进行排序/筛选时,如何确保行着色更新?

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

我正在写一个R闪亮的应用程序。我使用带有特定颜色的at DT datatable作为文本条目。重新调整表格时,颜色与正确的行不一致。相反,它们留在原地

我假设我需要观察表被重新排序/过滤的事件并对其作出反应。我该怎么做

下面是示例代码

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("&#9608;", "&#9608;", "&#9608;"))

  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">&#9608;</span>', rgbcolors)
dat <- data.frame(src=c(1,2,3), tgt=tgt)
datatable(dat, escape=FALSE)

mycolors不用回调,可以直接在
tgt
列中设置颜色。会不会有问题?太好了。非常感谢。