Home > Back-end >  Create datatable with editable row/column and observe changes in it
Create datatable with editable row/column and observe changes in it

Time:02-18

The DT documentation displays the possibility of creating editable rows, columns, and cells. When I use editable cells, everything works perfectly. I easily observe changes and could use them for my purposes. The problem is that I can't observe the changes in edited rows or columns. The question is how to find out the edited row number, column, and new value when using editable rows/columns? A simple example from DT (doesn`t work) from it is shown below. P.S. My DT version is 0.19, maybe its important

library(shiny)
library(DT)

dt_output = function(title, id) {
  fluidRow(column(
    12, h1(paste0('Table ', sub('.*?([0-9] )$', '\\1', id), ': ', title)),
    hr(), DTOutput(id)
  ))
}
render_dt = function(data, editable = 'cell', server = TRUE, ...) {
  renderDT(data, selection = 'none', server = server, editable = editable, ...)
}

shinyApp(
  ui = fluidPage(
    title = 'Double-click to edit table cells',

    dt_output('server-side processing (editable = "row")', 'x6')
  ),

  server = function(input, output, session) {
    d6 = iris
    d6$Date = Sys.time()   seq_len(nrow(d6))

    options(DT.options = list(pageLength = 5))

    # server-side processing
    output$x6 = render_dt(d6, 'row')
    
    # edit a row
    observeEvent(input$x6_cell_edit, {
      print("ping")
      d6 <<- editData(d6, input$x6_cell_edit, 'x6')
    })
  }
)

CodePudding user response:

This is a shiny app showing a editable data table and another table of edits:

library(shiny)
library(DT)
library(tidyverse)

shinyApp(
  ui = basicPage(
    h1("Data"),
    DTOutput("table"),
    h1("Edits"),
    DTOutput("edits")
  ),
  server = function(input, output, session) {
    edits <- reactiveVal(value = tibble(
      row = numeric(),
      col = numeric(),
      value = character()
    ))
    
    output$table <- renderDT(iris, editable = TRUE)
    output$edits <- renderDT(edits())
    
    observeEvent(input$table_cell_edit, {
      edits() %>%
        add_row(input$table_cell_edit) %>%
        edits()
    })
  }
)

enter image description here

CodePudding user response:

Here is a print(input$x6_cell_edit)

>      row col                value
>     1   1   0                    1
>     2   1   1                  5.1
>     3   1   2                    5
>     4   1   3                  1.4
>     5   1   4                  0.2
>     6   1   5               setosa
>     7   1   6 2022-02-17T12:51:35Z

I edited the first observation, column "Sepal.width".

  • Related