I think the simplest way to do this is to just add
options = list(stateSave = TRUE)
inside the renderDT()
function. Then, within the server
, the state of the table can be accessed at any time with input$<tableID>_state
(my table is just called "table" so this becomes input$table_state
:
observeEvent(input$table_state, {
str(input$table_state)
})
The whole solution is then:
library(shiny)
library(shinydashboard)
library(plotly)
library(DT)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
box(DTOutput("table"))
)
)
server <- function(input, output, session) {
fileData <- reactiveFileReader(1000, session, 'www/test.csv', read.csv)
output$table <- renderDT(fileData(), filter = "top",
options = list(stateSave = TRUE))
observeEvent(input$table_state, {
str(input$table_state)
})
}
shinyApp(ui, server)
Sample output within the RStudio console:
List of 6
$ time : num 1.54e+12
$ start : int 0
$ length : int 10
$ order : list()
$ search :List of 4
..$ search : chr ""
..$ smart : logi TRUE
..$ regex : logi FALSE
..$ caseInsensitive: logi TRUE
$ columns:List of 5
..$ :List of 2
.. ..$ visible: logi TRUE
.. ..$ search :List of 4
.. .. ..$ search : chr ""
.. .. ..$ smart : logi TRUE
.. .. ..$ regex : logi FALSE
.. .. ..$ caseInsensitive: logi TRUE
..$ :List of 2
.. ..$ visible: logi TRUE
.. ..$ search :List of 4
.. .. ..$ search : chr "[\"0\"]"
.. .. ..$ smart : logi TRUE
.. .. ..$ regex : logi FALSE
.. .. ..$ caseInsensitive: logi TRUE
..$ :List of 2
.. ..$ visible: logi TRUE
.. ..$ search :List of 4
.. .. ..$ search : chr "[\"8\"]"
.. .. ..$ smart : logi TRUE
.. .. ..$ regex : logi FALSE
.. .. ..$ caseInsensitive: logi TRUE
..$ :List of 2
.. ..$ visible: logi TRUE
.. ..$ search :List of 4
.. .. ..$ search : chr ""
.. .. ..$ smart : logi TRUE
.. .. ..$ regex : logi FALSE
.. .. ..$ caseInsensitive: logi TRUE
..$ :List of 2
.. ..$ visible: logi TRUE
.. ..$ search :List of 4
.. .. ..$ search : chr ""
.. .. ..$ smart : logi TRUE
.. .. ..$ regex : logi FALSE
.. .. ..$ caseInsensitive: logi TRUE
Note the search
lists which show the filters applied to each column.
Bonus
For super-easy filter extraction, use input$table_search_columns
. This gives the same result as using sapply
:
sapply(input$table_state$columns, function(x) x$search$search)
This will give something like
[1] "" "[\"0\"]" "[\"8\"]" "" ""
for the above example.