Somehow I managed to figure out the solution for my question. Posting it here, so it might help others. I got help from couple of places. Datatable document and stackoverflow.
Using these helps, I extended my selectall button functionality and also extended it for deselectall button (deselect any filtered rows).
Here's the updated shiny app
Below is the updated code:
library(shiny)
library(DT)
data(mpg)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Select only filtered rows using selectall button"),
br(),
br(),
DT::dataTableOutput("table")
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$table <- DT::renderDataTable({
datatable(mpg, escape=F,
rownames=F,
filter = 'top',
# colnames = c("Data Type","Variable","Description", "Filename"),
class = "compact hover row-border",
extensions = c('Scroller','Select', 'Buttons'),
options = list(
select = list(style = "multi", items = "row"),
columnDefs = list(list(className = 'dt-center', targets = "_all")),
language = list(
info = 'Showing _START_ to _END_ of _TOTAL_ variables'),
deferRender = TRUE,
scrollY = 500,
scroller = TRUE,
dom = "Blfrtip",
buttons = list(list(extend='selectAll',className='selectAll',
text="select all rows",
action=DT::JS("function () {
var table = $('#DataTables_Table_0').DataTable();
table.rows({ search: 'applied'}).deselect();
table.rows({ search: 'applied'}).select();
}")
), list(extend='selectNone',
text="DeselectAll",
action=DT::JS("function () {
var table = $('#DataTables_Table_0').DataTable();
table.rows({ search: 'applied'}).select();
table.rows({ search: 'applied'}).deselect();
}")
))
),
selection="none"
) }, server = F
)
}
# Run the application
shinyApp(ui = ui, server = server)
Hope this help others.