In a Shiny app I have a {DT} in which I update the data via the replaceData(dataTableProxy, data, ...)
paradigm. Once the data have been replaced, I'd like to (conditionally) select a row. The straight-forward approach looks like so:
output[["dt"]] <- DT::renderDataTable(my_dt, server = TRUE)
dt_proxy <- DT::dataTableProxy("dt", deferUntilFlush = TRUE)
# ...
shiny::observe({
new_data <- some_new_reactive_data()
DT::replaceData(dt_proxy, new_data, clearSelection = "all") # clear selection because we'll highlight a new row
DT::selectRows(dt_proxy, my_reactive_row_index(), ignore.selectable = TRUE)
})
After banging my head against the wall for a long while trying to figure out why the selectRows()
call wasn't selecting the row, I managed to figure out that within the browser the selectRows()
method was executing before the replaceData()
method on the DataTable. The net result is the row gets selected then is immediately cleared by the replaceData()
execution.
I could conceivably disable the clearSelection
argument in the replaceData()
call, but this seems risky to me as I also want to ensure that only the specified rows in my selectRows()
call are selected. Clearing the rows first via two successive selectRows()
calls seems like it could also then work, but it's the same challenge as with the original case: I'm now unsure if there's a guaranteed order-of-execution on the browser of the DT's proxied calls.
Has anyone encountered this async/out-of-order behavior before with {DT}; and know of a way to try to guarantee ordering browser-side?