input$table_rows_selected Returns Incorrect Indexes with Large Data & Slow Connection when Filter is set by default
Description:
When a default filter is set on a DT::datatable (options -> searchCols -> search), the input$table_rows_selected can return incorrect row indexes, especially with large data frames (10k+ rows) and a slow internet connection (e.g., Slow 3G).
Steps to Reproduce:
Use the provided R code to set up a Shiny app with a DT::datatable.
Throttle the connection to "Slow 3G" in browser DevTools.
Refresh the page.
Click on any row in the table.
Expected Behavior:
The R console should print the correct row ID of the selected row.
HTMLWidgets.widget({
name: "datatables",
type: "output",
renderOnNullValue: true,
initialize: function(el, width, height) {
...
},
renderValue: function(el, data, instance) {
...
// use the dataSrc function to pre-process JSON data returned from R
var DT_rows_all = [], DT_rows_current = [];
if (server && HTMLWidgets.shinyMode && typeof options.ajax === 'object' &&
/^session\/[\da-z]+\/dataobj/.test(options.ajax.url) && !options.ajax.dataSrc) {
options.ajax.dataSrc = function(json) {
DT_rows_all = $.makeArray(json.DT_rows_all);
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
DT_rows_current = $.makeArray(json.DT_rows_current);
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
var data = json.data;
if (!colReorderEnabled()) return data;
var table = $table.DataTable(), order = table.colReorder.order(), flag = true, i, j, row;
for (i = 0; i < order.length; ++i) if (order[i] !== i) flag = false;
if (flag) return data;
for (i = 0; i < data.length; ++i) {
row = data[i].slice();
for (j = 0; j < order.length; ++j) data[i][j] = row[order[j]];
}
return data;
};
}
...
})
The line DT_rows_current = $.makeArray(json.DT_rows_current); get called 3 times, and depending on the internet speed the output can differ. (DT_rows_current is directly used for determining indexes of input$table_rows_selected)
Below you can see DT_rows_current values with slow and normal connection:
[X] I have provided the necessary information about my issue.
If I'm asking a question, I have already asked it on Stack Overflow or RStudio Community, waited for at least 24 hours, and included a link to my question there.
If I'm filing a bug report, I have included a minimal, self-contained, and reproducible example, and have also included xfun::session_info('DT'). I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version: remotes::install_github('rstudio/DT').
If I have posted the same issue elsewhere, I have also mentioned it in this issue.
[X] I have learned the Github Markdown syntax, and formatted my issue correctly.
I understand that my issue may be closed if I don't fulfill my promises.
input$table_rows_selected
Returns Incorrect Indexes with Large Data & Slow Connection when Filter is set by defaultDescription:
When a default filter is set on a DT::datatable (options -> searchCols -> search), the input$table_rows_selected can return incorrect row indexes, especially with large data frames (10k+ rows) and a slow internet connection (e.g., Slow 3G).
Steps to Reproduce:
Use the provided R code to set up a Shiny app with a DT::datatable. Throttle the connection to "Slow 3G" in browser DevTools. Refresh the page. Click on any row in the table.
Expected Behavior:
The R console should print the correct row ID of the selected row.
Actual Behavior:
The R console prints incorrect rows_selected IDs.
Code to Reproduce:
xfun::session_info('DT')
Where is the problem?
As I understand the problem happens because of a race condition (see datatables.js Line 325 https://github.com/rstudio/DT/blob/main/inst/htmlwidgets/datatables.js#L325)
The line
DT_rows_current = $.makeArray(json.DT_rows_current);
get called 3 times, and depending on the internet speed the output can differ. (DT_rows_current is directly used for determining indexes of input$table_rows_selected)Below you can see DT_rows_current values with slow and normal connection:
By filing an issue to this repo, I promise that
xfun::session_info('DT')
. I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version:remotes::install_github('rstudio/DT')
.I understand that my issue may be closed if I don't fulfill my promises.