簡體   English   中英

如何在添加新列 R shiny 反應數據表之前保存更改

[英]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'))
  }))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM