R Shiny - 使用列排序禁用数据表中的特定行

R Shiny - Disabling specific rows in a datatable with column sorting

下面的应用程序包含启用了行选择的 iris 数据集的数据 table。我想专门禁用前 3 行的选择。我可以使用 here 发布的解决方案来做到这一点。当 table 在应用程序启动时初始化时,该解决方案工作正常:

但是,当您对列中的行进行排序时,例如在 Species 上按降序排列,它禁用观测值 101、102 和 103,因为它们现在是 table 的前 3 行,作为排序的结果:

我猜这是因为 rowCallbackdisplayIndex 禁用行。所以在启动时,displayIndex 0、1 和 2 分别对应于 iris 数据集中的观察值 1、2 和 3,并且行回调禁用它们的行选择,因为 indices.indexOf(displayIndex) > -1true行。但是在 Species 上排序后,displayIndex 0、1 和 2 分别对应于观察值 101、102 和 103,因此回调禁用了它们。

为了解决这个问题,我尝试通过将 rowCallback 函数中的 displayIndex 更改为 dataIndex 来禁用基于行名的行。但是,这会有些奇怪,每次我在不同的列上过滤时都会禁用其他行。这是 table 首先在 Sepal.Length 排序之后的示例,然后是 Sepal.Length 最后在 Petal.Length:

下面是重现上述内容的代码:

library(DT)
library(shiny)

disabled_rows = c(1,2,3)

#NOTE: displayIndex changed to dataIndex
rowCallback <- c(
  "function(row, data, displayNum, dataIndex){",
  sprintf("  var indices = [%s]", toString(disabled_rows - 1)),
  "  if(indices.indexOf(dataIndex) > -1){",
  "    $(row).find('td').addClass('notselectable').css({'background-color': '#eee', 'color': '#bbb'});",
  "  }",
  "}"
)

get_selected_rows <- c(
  "var id = $(table.table().node()).closest('.datatables').attr('id');",
  "table.on('click', 'tbody', function(){",
  "  setTimeout(function(){",
  "    var indexes = table.rows({selected:true}).indexes();",
  "    var indices = Array(indexes.length);",
  "    for(var i = 0; i < indices.length; ++i){",
  "      indices[i] = indexes[i];",
  "    }",
  "    Shiny.setInputValue(id + '_rows_selected', indices);",
  "  }, 0);",
  "});"
)

drag_selection <- c(
  "var dt = table.table().node();",
  "$(dt).selectable({",
  "  distance : 10,",
  "  selecting: function(evt, ui){",
  "    $(this).find('tbody tr').each(function(i){",
  "      if($(this).hasClass('ui-selecting')){",
  "        table.row(i).select();",
  "      }",
  "    });",
  "  }",
  "}).on('dblclick', function(){table.rows().deselect();});"
)

shinyApp(
  ui = fluidPage(
    tags$head(tags$script(src = "https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js")),
    DTOutput('table')
  ),

  server = function(input, output, session) {  

    output$table <- renderDT({

        datatable(
          iris,
          callback = JS(get_selected_rows),
          class = 'hover row-border order-column',
          options = list(
          rowCallback = JS(rowCallback), 
          select = list(style = "multi", selector = "td:not(.notselectable)")
          ), 
        extensions = "Select", selection = 'none'
        )
    }, server = F)

    observe({

      print(input$table_rows_selected)

    })
  }
)

我不知道为什么将 displayIndex 更改为 dataIndex 不起作用,我不知道还能尝试什么。 dataIndex 参数 here 的 DataTables 定义对我来说很晦涩,因为我不太熟悉 DT 插件,所以我将不胜感激任何帮助。

编辑:我也试过直接基于行名禁用如下:

rowCallback <- c(
  "function(row, data, displayNum, displayIndex){",
  "  var indices = [0, 2, 4, 15]",
  "  if(indices.indexOf(data[0]) > -1){",
  "    $(row).find('td').addClass('notselectable');",
  "  }",
  "}"
)

很奇怪,dataIndex不行。

您可以为行使用一些 ID。

disabled_rows = paste0("'", paste0("row", c(1,2,3)), "'")

rowCallback <- c(
  "function(row, data, displayNum, displayIndex){",
  sprintf("  var indices = [%s];", toString(disabled_rows)),
  "  if(indices.indexOf($(row).attr('id')) > - 1){",
  "    $(row).find('td').addClass('notselectable').css({'background-color': '#eee', 'color': '#bbb'});",
  "  }",
  "}"
)

dat <- iris
dat$ID <- paste0("row", 1:nrow(iris))
rowNames <- TRUE
colIndex <- as.integer(rowNames)


output$table <- renderDT({

  datatable(
    dat,
    rownames = rowNames,
    callback = JS(get_selected_rows),
    class = 'hover row-border order-column',
    options = list(
      rowId = JS(sprintf("function(data){return data[%d];}", 
                         ncol(dat)-1+colIndex)),
      rowCallback = JS(rowCallback), 
      select = list(style = "multi", selector = "td:not(.notselectable)")
    ), 
    extensions = "Select", selection = 'none'
  )
}, server = TRUE)