I’m trying to create a datatable with pagination in R with checkboxes that are preselected. Other examples (eg here) do not account for pagination.
In the following example, there checkbox status gets reset when you return to a page. In addition, the variable excludedrows does not count the rows checked on other pages.
library(shiny)
library(DT)
ui = fluidPage(
tags$script(HTML('$(document).on("click", "input", function () {
var checkboxes = document.getElementsByName("row_selected");
var checkboxesChecked = [];
for (var i=0; i<checkboxes.length; i++) {
if (checkboxes[i].checked) {
checkboxesChecked.push(checkboxes[i].value);
}}
Shiny.onInputChange("checked_rows",checkboxesChecked);
})')),
verbatimTextOutput("excludedRows"),
DTOutput('myDT')
)
server = function(input, output) {
mymtcars_reactive <- reactive(mtcars)
output$myDT <- renderDataTable({
mymtcars <- mymtcars_reactive()
mymtcars[["Select"]] <- paste0('<input type="checkbox" name="row_selected" value=',1:nrow(mymtcars),' checked>')
datatable(mymtcars,selection = "multiple",
options = list(pageLength = 14,
lengthChange = FALSE,
stateSave = TRUE),
rownames= FALSE,
escape=F)
})
output$excludedRows <- renderPrint({
intersect(input$checked_rows,1:nrow(mymtcars_reactive()))
})
}
shinyApp(ui,server, options = list(launch.browser = TRUE)
server = FALSE
inDT::renderDataTable
? - Stéphane Laurentserver = FALSE
solves the problem of the checked status being reset, but does not give the correctinput$checked_rows
value, which still only counts the checked items on the current page. - fifthace