Shiny: keep boxes checked on datatable after changing inputs

Viewed 1155

I want to implement checkboxes into my shiny app; however, I'm facing two problems:

  1. After I reorder columns, any checks on the datatable disappear (e.g., try to order table by mpg)
  2. After I remove column, any checks on the datatable disappear (e.g., unchecking boxes from Columns to show:)

Here's my dummy example (it's a modified version of code from this SO answer):

library(shiny)
TABLE = mtcars
TABLE$id = 1:nrow(mtcars)
APP <- list()

APP$ui <- pageWithSidebar(
    headerPanel(NULL),
    sidebarPanel(
        checkboxGroupInput("show_vars", "Columns to show:", 
                           names(TABLE), selected = names(TABLE))
    ),
    mainPanel(
        dataTableOutput("resultTABLE")
    )
)
APP$server <- function(input, output, session) {

    output$resultTABLE = renderDataTable({
        addCheckboxButtons <- paste0('<input type="checkbox" name="row', 
                                     TABLE$id, '" value="', TABLE$id, '">',"")
        cbind(Pick = addCheckboxButtons, TABLE[, input$show_vars, drop = FALSE])
    }, escape = FALSE)
}

runApp(APP)

APP works, but for the full implementation I need to solve problems 1 and 2.

1 Answers
Related