How to save sorting in dataTable in brilliant?

I have a table on the page with possible sorting in columns, after I reload the data with the reactive table, it is not sorted again, here is the server.R code:

    library(shiny)

shinyServer(function(input, output) {

  # Return the requested dataset
  datasetInput <- reactive({
    switch(input$dataset2,
           "[,...]" = diamonds,
           "[10:30,...]" = diamonds,
           "[31:50,...]" = diamonds)
  })

  #"[,...]" = diamonds[,],
  #"[10:30,...]" = diamonds[10:30,],
  #"[31:50,...]" = diamonds[31:50,])

  # Show the first "n" observations
  output$view <- renderTable({
    head(datasetInput())
  })

  # a large table, reative to input$show_vars
  output$mytable1 <- renderDataTable({
    library(ggplot2)
    datasetInput()[, input$show_vars, drop = FALSE]
  })

})
+4
source share
1 answer

The best way to keep the sort order (and choice) is to use proxyDataTable and replaceData to update your data, rather than creating new tables every time the data is updated:

mydata = reactive({
    df$ID <<- c(df$ID[n], df$ID[-n])
    df
  })
  output$foo = DT::renderDataTable(isolate(mydata()))
  proxy = dataTableProxy('foo')

  observe({
    replaceData(proxy, mydata(), resetPaging = FALSE)
  })

, . , , , : https://github.com/rstudio/DT/issues/359

, rownames = false datatable, replaceData.

proxyDataTable , DT:: renderDataTable:

#include some javascript
tags$head(tags$script(src="my.js"))

#options to renderDataTable
 preDrawCallback = JS("initTableOrder"),
 drawCallback = JS("saveTableOrder")

javascript my.js( www):

var tableSortOrderSave;

function initTableOrder(settings, json) {   
  if(tableSortOrderSave === undefined){
    $(this.api().table().order());
  }else{
    $(this.api().table().order(tableSortOrderSave));
  }
}

function saveTableOrder(settings, json) {
  order  = $(this.api().table().order());
  if(order.length > 0){
    tableSortOrderSave = order;
  }
}
0

All Articles