问题
The app below contains a datatable of the iris
dataset with row selection enabled. I would like to disable selection for the first 3 rows specifically. I can do this using the solution posted here. The solution works fine when the table initialises on app startup:
However, when you sort the rows on a column, e.g. on Species
in descending order, it disables observations 101, 102 and 103 since they are now the first 3 rows of the table as a result of the sorting:
I am guessing this happens because rowCallback
is displayIndex
to disable rows. So on startup, displayIndex
0, 1 and 2 correspond to observations 1, 2 and 3 respectively in the iris dataset and the row callback disables row selection for them since indices.indexOf(displayIndex) > -1
is true
for these rows. But after sorting on Species
, displayIndex
0, 1 and 2 correspond to observations 101, 102 and 103 respectively and so the callback disables them instead.
To fix this, I tried disabling rows based on rowname by changing the displayIndex
in the rowCallback
function to dataIndex
. However, this does something weird where additional rows get disabled each time I filter on a different column. Here is an example after the table was first sorted on Sepal.Length
, then Sepal.Length
and then finally on Petal.Length
:
Here is the code to reproduce the above:
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)
})
}
)
I don't know why changing displayIndex
to dataIndex
isn't working and I don't know what else to try. The DataTables definition of the dataIndex
parameter here is obscure to me since I am not all that familiar with the DT plug-in, so I would appreciate any help.
EDIT: I also tried disabling based on rowname directly as follows:
rowCallback <- c(
"function(row, data, displayNum, displayIndex){",
" var indices = [0, 2, 4, 15]",
" if(indices.indexOf(data[0]) > -1){",
" $(row).find('td').addClass('notselectable');",
" }",
"}"
)
回答1:
It's strange that dataIndex does not work.
You can use some id for the rows instead.
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)
来源:https://stackoverflow.com/questions/57505250/r-shiny-disabling-specific-rows-in-a-datatable-with-column-sorting