This is cross-posted to stack overflow but I find I often get more response over here. I have a shiny app that uses DT to display some data. At some point, I have a list of rows (row numbers) I want to navigate directly to using a button attached to the datatable object. I've come up with a method of doing it, but for some reason, it only navigates to some rows and not to others. Strangely, the rows it refuses to navigate to seem consistent over and over. If I re-run the app, it will fail to navigate to the same ones each time. In the attached example, the rows I want to jump to are 8, 10, 29, 30, 68, 78, 99, 103, 104, 108, 109, 112, 120, 122, 128. For some reason, once it gets to 109, the scrollTo
call does nothing until the number cycles back to 8. I initially tried this with the numbers in random order and thinking they needed to be sorted, I tried that, with the same result. Interestingly, the rows that don't work are the highest 5 (109, 112, 120, 122, and 128). I can't see any reason why it would be behaving this way. Anybody have any insight?
Also, if there's a better way of doing this, I'd love to know. It does seem a little kludgy to send the row numbers by message and then loop through them elsewhere in javascript.
Run the example below and hit the next button a bunch of times. You'll (hopefully) see that it reaches a point where the datatable no longer jumps to the next row until the index cycles back to the beginning. These numbers are all within the max rows of the dataframe (iris).
library(shiny)
library(DT)
ui <- shinyUI({
fluidPage(
tags$head(
tags$script("
var row_index = 0;
var view_rows = [];
Shiny.addCustomMessageHandler('set-rows',function(rows) {
view_rows = rows;
row_index = 0;
})
")
),
textOutput("row_list"),
DTOutput("tabs")
)
})
server <- shinyServer(function(input,output,session) {
view_rows <- reactive({
c(111, 9, 108, 77, 67, 7, 103, 28, 98, 29, 121, 119, 127, 102, 107)
# sample(1:nrow(iris),15)
})
output$row_list <- renderText({
paste(view_rows()+1,collapse=", ")
})
observe({
print("sending rows")
session$sendCustomMessage("set-rows",view_rows())
})
output$tabs <- renderDT({
datatable(
iris,
extensions = c("Buttons","Scroller"),
options = list(
dom = 'BfrtSip',
scroller = TRUE,
selection = "single",
scrollY = "60vh",
buttons = list(
list(
extend = "collection",
text = "next",
action = DT::JS("
function ( e, dt, node, config ) {
console.log('going to ' + view_rows[row_index]);
dt.row(view_rows[row_index++]).scrollTo(false);
if (row_index >= view_rows.length) {
row_index = 0;
}
}
")
)
)
)
)
})
})
shinyApp(ui,server)