如何避免数据表中行回调和排序之间的混淆

Mar*_*ark 5 datatable r shiny dt

在上一个问题的帮助下,我现在可以设置所选行的样式(旨在让用户选择要从进一步分析中排除的行),但我发现在执行排除行的功能后对数据表进行排序(使它们变灰)并添加一个不同的图标,将图标保留在正确的行中,但将错误的行灰显。

这是在排序前取消选择第 2,3 和 4 行后的表格: 在此处输入图片说明

排序后:(在正确的行有十字,但不会变灰。

在此处输入图片说明

   library(shiny)
      library(DT)

  mtcars <- as.data.table(mtcars[1:15, )
  ui <- fluidPage(
    # actionButton('SubmitRemoval', 'Exclude selected rows'),
    # actionButton('UndoRemoval', 'Include full data'),
    # br(),
    DTOutput('metadataTable')

  )

  server <- function(input, output,session) {

    values <- reactiveValues()

    rowCallbackMeta = function(rows){
      c(
        "function(row, data, num, index){",
        sprintf("  var rows = [%s];", paste0(rows-1, collapse = ",")),
        "  if(rows.indexOf(num) > -1){",
        "    for(var i=0; i<data.length; i++){",
        "      $('td:eq('+i+')', row)",
        "        .css({'color': 'rgb(211,211,211)', 'font-style': 'italic'});",
        "    }",
        "  }",
        "    $('td:eq(3)', row).html(data[3].toExponential(2));",
        "}"  
      )
    }
    output$metadataTable <-  DT::renderDataTable({

      rows <- values$RowsRemove

      # mtcars1 <- cbind(Selected ='<span style = "color:#31C769 ; font-size:18px"><i class="fa fa-check"></i></span>', mtcars)
      mtcars1 <- cbind(Selected ='<span style = "color:red ; font-size:18px"><i class="glyphicon glyphicon-ok"></i></span>', mtcars)

      print(rows)
      # if(!is.null(rows)) { 
      mtcars1$Selected[rows] <- '<span style = "color:red ; font-size:18px"><i class="glyphicon glyphicon-remove"></i></span>' 
      # }

      Table_opts <- list(
        dom = 'frtipB',
        searching = F,
        pageLength = 50,
        searchHighlight = TRUE,
        colReorder = TRUE,
        fixedHeader = TRUE,
        buttons = list('copy', 'csv',
                       list(
                         extend = "collection",
                         text = 'Deselect', 
                         action = DT::JS("function ( e, dt, node, config ) {
                                       Shiny.setInputValue('SubmitRemoval', true, {priority: 'event'});
                                     }")
                       ),
                       list(
                         extend = "collection",
                         text = 'Restore', 
                         action = DT::JS("function ( e, dt, node, config ) {
                                       Shiny.setInputValue('UndoRemoval', true, {priority: 'event'});
                                     }")
                       )
        ),
        paging    = TRUE,
        deferRender = TRUE,
        columnDefs = list(list(className = 'dt-right', targets = '_all')),
        rowCallback = JS(rowCallbackMeta(rows)),
        scrollX = T,
        scrollY = 440
      )
      DT::datatable(mtcars1, 
                    escape = FALSE, 
                    extensions = c('Buttons', 'ColReorder', 'FixedHeader', 'Scroller'),
                    selection = c('multiple'),
                    rownames = FALSE
                    ,
                    options = Table_opts
      )
    })


    observeEvent(values$RowsRemove, {
      print('seeing rows remove')
      values$Datafiles_meta_Selected  <-  values$Datafiles_meta_Selected[-c(values$RowsRemove),]
    })

    observeEvent(input[['SubmitRemoval']], { 
      if(is.null(values$RowsRemove)) { values$RowsRemove <- as.numeric()}
      values$RowsRemove <- unique(c(values$RowsRemove, input[["metadataTable_rows_selected"]]))
    })

    observeEvent(input[["UndoRemoval"]], { 
      values$RowsRemove <- NULL
      values$Datafiles_meta_Selected <- values$Datafiles_meta
    })

  }

  shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)

Nic*_*icE 2

num在 JavaScript 中使用的选择要灰显的行是基于当前显示上的行号,因此不会受到排序的影响。

您可以尝试通过if以下方式替换函数中的语句rowCallbackMeta

if(data[0].search('remove') > -1)
Run Code Online (Sandbox Code Playgroud)

这会在数据的第一列中查找“删除”以排除行,并且有效,因为第一列中的字形图标会更新为<i class="glyphicon glyphicon-remove"></i>排除行时的值。