0

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, 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!

burchz
  • 1

1 Answers1

0

SOLVED...

Thanks to this post... R Shiny - multi-page editable DataTable jumps to row #1 after an edit

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
    })
  }
)
burchz
  • 1