R Shiny DT - edit values in table with reactive

前端 未结 2 1146
猫巷女王i
猫巷女王i 2020-12-06 06:57

Is it possible to update a reactive data source by editing the DT::DataTable? Below code is based on this code with change that x is made reactive. The problem starts when

相关标签:
2条回答
  • 2020-12-06 07:14

    If you don't show the row names in your DT then you should add 1 to info$col to get the correct column i.e., j = info$col + 1.

    0 讨论(0)
  • 2020-12-06 07:18

    I am not sure if I understand you correctly, but maybe this solution might help you a bit. I changed your reactive into a reactiveValues object and I removed the replaceData line.

    library(shiny)
    library(DT)
    shinyApp(
      ui = fluidPage(
        DTOutput('x1'),
        verbatimTextOutput("print")
      ),
      server = function(input, output, session) {
        x = reactiveValues(df = NULL)
    
        observe({
          df <- iris
          df$Date = Sys.time() + seq_len(nrow(df))
          x$df <- df
        })
    
        output$x1 = renderDT(x$df, selection = 'none', editable = TRUE)
    
        proxy = dataTableProxy('x1')
    
        observeEvent(input$x1_cell_edit, {
          info = input$x1_cell_edit
          str(info)
          i = info$row
          j = info$col
          v = info$value
    
          # problem starts here
          x$df[i, j] <- isolate(DT::coerceValue(v, x$df[i, j]))
        })
    
        output$print <- renderPrint({
          x$df
        })
      }
    )
    
    0 讨论(0)
提交回复
热议问题