Shiny for R DT: how to read / retain page length and column visibility
<p>I am quite new to the application, so forgive me if it is obvious. I have a datatable that can be recalculated based on a map center. It works fine. However I would like to read and retain (preserve) certain settings: namely page length and columns visibility. At the moment if a user changes to 20 entries per page and moves the map, it re-renders it with default page length and column vis (using colvis in buttons). I thought i could store it in some variable and retrieve it when re-rendering the table, but not sure how to read from inside the options.
it is built in R Studio for Shiny
Any ideas?</p>

<p>EDIT: I got the data about the page length and column visibility using option stateSave = TRUE and then reading input$Btable_state$length (and i can read as well what columns are visible), but now my issue is: when re-rendering a table it goes to NULL value first, overwriting whatever is stored in input$Btable_state. How can i capture and affect it only if it is altered by changes to the filters, not by the activity of re-rendering the table itself?</p>

<p>EDIT2: as my application requires connection to my google sheets what would not be replicable it can be tried on following example from Github (vnijs/dt_state). How to preserve the nondefault page length (for example 50) when reordering columns (but without linking the action to the selectizeInput, just keeping the last correct state)</p>

<pre><code>#ui.r
shinyUI(fluidPage(
  sidebarLayout(
    sidebarPanel(
      uiOutput("ui_view_vars"),
      tags$a(id = "refresh", href = "#", class = "action-button",
             list(icon("refresh"), "Clear state"),
             onclick = "window.location.reload();")
    ),
    mainPanel(
      tabPanel("View", DT::dataTableOutput("dataviewer")

      )
    )
  )
))

#server.r
library(shiny)
library(DT)

dat &lt;- iris

## store state in global environment for now
if (!exists("r_state")) r_state &lt;- list()

## Only used from a 'clean' start
r_state$dataviewer_search_columns &lt;- list("","2.5...4","","","[\"setosa\"]")

shinyServer(function(input, output, session) {
  output$ui_view_vars &lt;- renderUI({
    vars &lt;- colnames(dat)

    ## using selectizeInput with drag_drop and DT
    selectizeInput("view_vars", "Select variables to show:", choices  = vars,
                   selected = vars, multiple = TRUE,
                   options = list(plugins = list('remove_button', 'drag_drop')))
  })

  ## make nested list
  mknl &lt;- function(x) list(search = x)

  observeEvent(input$dataviewer_search_columns, {
    r_state$dataviewer_search_columns &lt;&lt;- input$dataviewer_search_columns
  })

  observeEvent(input$dataviewer_state, {
    r_state$dataviewer_state &lt;&lt;- input$dataviewer_state
  })

  observeEvent(input$refresh, {
    r_state &lt;&lt;- list()
  })

  output$dataviewer &lt;- DT::renderDataTable({
    req(input$view_vars)

    search &lt;- r_state$dataviewer_state$search$search
    if (is.null(search)) search &lt;- ""

    DT::datatable(iris[,input$view_vars],
                  filter = list(position = "top", clear = TRUE),
                  rownames = FALSE,
                  selection = "none",
                  options = list(
                    stateSave = TRUE,
                    searchCols = lapply(r_state$dataviewer_search_columns, mknl),
                    search = list(search = search, regex = TRUE),
                    order = {if (is.null(r_state$dataviewer_state$order)) list()
                      else r_state$dataviewer_state$order},
                    processing = FALSE
                  ),
                  callback = DT::JS("$(window).unload(function() { table.state.clear(); })")
    )
  })

  output$tbl_col_search &lt;- renderPrint(input$dataviewer_search_columns)

})
</code></pre>