Ajax Error with Load Balancer on Chrome
I recently implemented a load balancer for my R Shiny application with two separate instances of the application running. On occasion when I navigate to the load balancer URL I see the following JS alert:
DataTables warning: table id=DataTables_Table_0 - Ajax error. For more information about this error, please see http://datatables.net/tn/7
In the console I see a 404 Not Found error on all my datatable objects. I am using S3 and grabbing all of my .rds files when the application loads. All other objects load as expected except those that will be rendered as a datatable. This only seems to occur on Chrome and only happens about 25% of the time - the error seems quite erratic. Is this a known issue or has anyone seen this happen before?
A simple workaround for your case is to use client mode for all your DT tables (if your table is not too big to use client mode).
As for the cause, it seems like the AJAX request is forwarded to the wrong worker, which I suppose is tied to your load balancer implementations.
https://stackoverflow.com/questions/26242541/ajax-request-and-load-balance this may help.
This is called sticky session or persistent session in load rebalancing.
Thank you - to confirm, by client mode you are referring to the below?
datatable(mtcars, rownames = FALSE, options = list(ajax = list(serverSide = FALSE)))
I tested the above and the issue still occurs around 10% of the time.
No, I mean DT::renderDT(..., server=False). See ?DT::renderDT
server : whether to use server-side processing. If TRUE, then the data is kept on the server and the browser requests a page at a time; if FALSE, then the entire data frame is sent to the browser at once. Highly recommended for medium to large data frames, which can cause browsers to slow down or crash.
Thank you. Disabling server-side processing seems to be affecting the input$dtname_rows_selected. With server = TRUE, I am able to output the index of the row selected, yet with server=FALSE the output of input$dtname_rows_selected is NULL.
No, it should not. Please test it on local. If there’s still a problem, a reproducible example is welcomed. Thanks.
I see this problem too . It occurs a few seconds after I open the page .
When I checkout the problem, it shows the post request return a 404 not found error.
DT version : 0.5
Hi @shrektan. After further investigation I see that the rows_selected functionality is actually operating correctly, but rather I instantiated an action button that when clicked will select the appropriate row in the datatable which is now failing went I set server = FALSE:
observe({
lapply (1:length(unique(my_dt$FIELD1)), function (i) {
observeEvent(input[["button"]], {
proxy = dataTableProxy('TABLE1')
proxy %>% selectRows(as.numeric(i))
})
})
})
The following code works for me. If you still have problems, please provide a minimal reproducible example so that we can help. Thanks.
library(shiny)
ui <- fluidPage(
actionButton('select', 'select'),
DT::DTOutput('tbl')
)
server <- function(input, output, session) {
output$tbl <- DT::renderDT(iris, server = FALSE)
proxy <- DT::dataTableProxy('tbl')
observeEvent(input$select, {
DT::selectRows(proxy, sample(1:10, 1))
})
}
shiny::runApp(mget(c('ui', 'server')))
Hello @shrektan. Your example does not seem to be working for me. I included a text output to your example to print the current selected row and when I set server=FALSE there is no text output, however when server=TRUE it seems to select a random row.
Does not select a random row on button click
library(shiny)
ui <- fluidPage(
actionButton('select', 'select'),
DT::DTOutput('tbl'),
htmlOutput('selectedrow')
)
server <- function(input, output, session) {
output$tbl <- DT::renderDT(iris, server = FALSE)
proxy <- DT::dataTableProxy('tbl')
observeEvent(input$select, {
DT::selectRows(proxy, sample(1:10, 1))
})
output$selectedrow <- renderText(input$tbl_rows_selected)
}
shiny::runApp(mget(c('ui', 'server')))
Selects a random row on button click
library(shiny)
ui <- fluidPage(
actionButton('select', 'select'),
DT::DTOutput('tbl'),
htmlOutput('selectedrow')
)
server <- function(input, output, session) {
output$tbl <- DT::renderDT(iris, server = TRUE)
proxy <- DT::dataTableProxy('tbl')
observeEvent(input$select, {
DT::selectRows(proxy, sample(1:10, 1))
})
output$selectedrow <- renderText(input$tbl_rows_selected)
}
shiny::runApp(mget(c('ui', 'server')))
Hi @shrektan. Just following up here - would you categorize this as a bug?
No, both versions work on my computer. Are you sure you are using the latest version of DT? If not, please install and re-test it by running remotes::install_github('Rstudio/DT').
If still had problems, please paste your sessionInfo() here. Thanks.
Thank you - I will test with the latest version. Are you able to update CRAN with the latest version?
We can update DT on CRAN this month if it would fix your issue.
Thank you - this seems to be working now. One final question - now that I have flipped my tables to client-side processing and try to filter on columns it is extremely slow (~15 - 20 seconds to respond). Is there any way to mitigate this slowless?
I’m afraid not.
Anyway, generally it should be fast enough unless your table contains dozens of columns and you are using IE...
The table contains 8 columns, however I am using IE. Are there any steps to remediate for IE use?
So your table contains tons of characters or thousands of rows? For small datasets, it should be fast. However, on some old computers, using IE is just 10 times slower.
I can only suggest you:
- Update your IE version (not sure if related but you can try to disable the compatible view mode),
- Use server processing mode,
- Or, use Chrome.
There are actually only 10 columns and roughly 700 rows. Understood - if there is any way to mitigate the ajax error without switching to client-side processing I will do that. Is that possible?
IMHO, you need to configure the sticky sessions for your load balancer. If you are using Nginx, adding ip_hash to the directive should be enough (I never test this but should be easy to google)...