R Shiny DT - edit values in table with reactive
Asked Answered
B

2

17

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 trying to change x in observeEvent.

The purpose of having x reactive is that I intend to source it from an external database, then have edits to the DT::DataTable write back to the database so that it stays in sync with what the user sees (I'm fine with doing that - it is not part of the question).

library(shiny)
library(DT)
shinyApp(
  ui = fluidPage(
    DTOutput('x1')
  ),
  server = function(input, output, session) {
    x = reactive({
      df <- iris
      df$Date = Sys.time() + seq_len(nrow(df))
      df
    })
    output$x1 = renderDT(x(), 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()[i, j] <<- isolate(DT::coerceValue(v, x()[i, j])) 
      replaceData(proxy, x(), resetPaging = FALSE)  # important
    })
  }
)
Braley answered 22/5, 2018 at 14:22 Comment(1)
I think you want to take a look at the package handsontableMaurya
B
23

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
    })
  }
)
Blackfish answered 22/5, 2018 at 14:47 Comment(3)
I'm having the issue that if you change column 1, info$col actually shows 0. Not sure why i get column numbers that are n -1 in my app. Anyone else seen this? I'll test tomorrow with this dummy app codeIncarnate
I have the same problem, but simply adding 1 to col doesn't work either.Tend
Indeed, this seems to happen when rownames are NULL, which causes an error when editing the first column and otherwise edits the wrong column. But adding 1 to j seems to fix it for me.Blackfish
C
5

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.

Cosset answered 26/2, 2019 at 14:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.