如何在添加新列 R 闪亮反应数据表之前保存更改

how to save changes before adding new column R shiny reactive datatable

编辑现有单元格后,如何使单元格值保持编辑状态而不是在添加新列时重置为原始值?尝试使用 reactiveValues 但没有成功。下面是可重现的代码,.

runApp(list(
  ui=pageWithSidebar(headerPanel("Adding entries to table"),
                     sidebarPanel(actionButton("update", "Add Column")),
                     mainPanel(DT::dataTableOutput("data"))),
  
  server=function(input, output, session) {
    df <- data.frame(Channel = c("A", "B","C"),
                     Current = c(2000, 3000, 4000),
                     Modified = c(2500, 3500,3000),
                     stringsAsFactors = FALSE)
    
    
    #smth <- reactiveValues(df2=integer(NROW(df)))
    values <- reactiveValues(df=df)
    proxyTable <<- DT::dataTableProxy("data")
    
    observeEvent(input$data_cell_edit, {
      info = input$data_cell_edit
      row = info$row
      col = info$col 
      value = info$value
      values$df[[row,col]] = value
      replaceData(proxyTable, values$df) 
    })
    
    newEntry <- observe({
      
      if(input$update > 0) {
        isolate({
          values$df[,paste0('NewCol', ncol(values$df) + 1)] <- integer(NROW(df))
        })
      }
    })
    output$data <- DT::renderDataTable(DT::datatable({values$df}, extensions = "AutoFill", editable = 'cell',
                                                     options = list(autoFill=list(focus="click"))),server=FALSE)
  }))

就像我说的,您需要在编辑 时更新数据,这就是这段代码的作用。

library(shiny)
library(DT)

runApp(list(
  ui=pageWithSidebar(headerPanel("Adding entries to table"),
                     sidebarPanel(actionButton("update", "Add Column")),
                     mainPanel(DT::dataTableOutput("data"))),

  server=function(input, output, session) {
    df <- data.frame(Channel = c("A", "B","C"),
                     Current = c(2000, 3000, 4000),
                     Modified = c(2500, 3500,3000),
                     stringsAsFactors = FALSE)
    
    
    smth <- reactiveValues(df2=integer(NROW(df)))
    values <- reactiveValues(df=df)
    proxyTable <<- dataTableProxy("data")

    observeEvent(input$data_cell_edit, {
      info = input$data_cell_edit
    row = info$row
    col = info$col 
    value = info$value
    values$df[[row,col]] = value
      replaceData(proxyTable, values$df) 
    })

    newEntry <- observe({
     
      if(input$update > 0) {
        isolate({
        values$df[,paste0('NewCol', ncol(values$df) + 1)] <- smth$df2
        })
      }
    })
    output$data <- DT::renderDataTable(DT::datatable({values$df}, editable = 'cell'))
  }))