rshinydt

Shiny : Make edits persist across pages in editable datatable


I have an editable datatable which is paginated as follows :

d1 = file.df
output$file.df_data<-DT::renderDataTable(
      d1,selection = 'none', editable = list(target = "cell", disable = list(columns = c(which(names(d1) != "product_type")-1))), 
      rownames = FALSE,
      extensions = 'Buttons',
      
      options = list(
        paging = TRUE,
        searching = TRUE,
        fixedColumns = TRUE,
        autoWidth = TRUE,
        ordering = TRUE,
        dom = 'Bfrtip',
        buttons = c('csv', 'excel')
      ),
      
      class = "display"
    )

When I make an edit on the current page, move to some other page, and then return to the previous page the edits that I had made on the page disappear. How can I make the edits persist across the pages?

Following is the code I am using to observe edits-

observeEvent(input$file.df_data_cell_edit, {
      d1[input$file.df_data_cell_edit$row,input$file.df_data_cell_edit$col+1] <<- input$file.df_data_cell_edit$value
    })

enter image description here


Solution

  • You have to use a proxy and the editData function:

    library(shiny)
    library(DT)
    
    ui <- basicPage(
      br(),
      DTOutput("dtable")
    )
    
    server <- function(input, output, session){
      
      dat <- iris
      
      output[["dtable"]] <- renderDT({
        datatable(dat, editable = TRUE)
      })
      
      proxy <- dataTableProxy("dtable")
      
      observeEvent(input[["dtable_cell_edit"]], {
        info <- input[["dtable_cell_edit"]]
        dat <<- editData(dat, info, proxy)
      })
      
    }
    
    shinyApp(ui, server)