Datatable - Editing values, impacting other columns, and retaining paging

Hello all,

I'm trying to allow the user to edit DT values, and based on those edits, run calculations on other columns. That works great on page one, but editing values on subsequent pages resets the paging back to the first page - a nightmare from a UX perspective.

Sample App demonstrating problem.
Try editing a vs value on Page 2 to see what happens...

library(shiny)
library(DT)
library(dplyr)
shinyApp(
    ui = fluidPage(
        DTOutput('table')
    ),
    server = function(input, output, session) {
        x = reactiveValues(df = mtcars %>% select(vs,am))
        
        output$table = renderDT(x$df, editable = TRUE)
        
        proxy = dataTableProxy('table')
        
        observeEvent(input$table_cell_edit, {
            info = input$table_cell_edit
            str(info)
            i = info$row
            j = info$col
            v = info$value
            
            x$df[i, j] <- isolate(DT::coerceValue(v, x$df[i, j]))
            x$df[i,which(colnames(x$df)=='am')] <- x$df[[i, j]]*2
            replaceData(proxy, x$df, resetPaging = FALSE)  # important I have tried with and without this line no impact on page resetting
        })
    }
)

Based on this post on Stack Overflow, I tried removing the reactivity, but it won't accept multiple edits. Try making two edits to vs on the first page to see what happens...

library(shiny)
library(DT)
library(dplyr)
shinyApp(
    ui = fluidPage(
        DTOutput('table')
    ),
    server = function(input, output, session) {
        
        df <- mtcars %>% select(vs,am)
        
        output$table = renderDT(df, editable = TRUE)
        
        proxy = dataTableProxy('table')
        
        observeEvent(input$table_cell_edit, {
            info = input$table_cell_edit
            str(info)
            i = info$row
            j = info$col
            v = info$value
            
            df[i, j] <- isolate(DT::coerceValue(v, df[i, j]))
            df[i,which(colnames(df)=='am')] <- df[[i, j]]*2
            replaceData(proxy, df, resetPaging = FALSE)
        })
    }
)

Any pointers would be GREATLY appreciated!

SOLVED...

Thanks to this post

Solution is to isolate the reactive df in the renderDT() function like this...

library(shiny)
library(DT)
library(dplyr)
shinyApp(
  ui = fluidPage(
    DTOutput('table')
  ),
  server = function(input, output, session) {
    x = reactiveValues(df = mtcars %>% select(vs,am))
    
    output$table = renderDT(isolate(x$df), editable = TRUE)
    
    proxy = dataTableProxy('table')
    
    observeEvent(input$table_cell_edit, {
      info = input$table_cell_edit
      str(info)
      i = info$row
      j = info$col
      v = info$value
      
      x$df[i, j] <- isolate(DT::coerceValue(v, x$df[i, j]))
      x$df[i,which(colnames(x$df)=='am')] <- x$df[[i, j]]*2
      replaceData(proxy, x$df, resetPaging = FALSE)  # important I have tried with and without this line no impact on page resetting
    })
  }
)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.