【问题标题】:R shiny app - Edit value in DT and updateR 闪亮的应用程序 - 在 DT 中编辑值并更新
【发布时间】:2019-07-15 15:29:12
【问题描述】:

我有一个闪亮的应用程序,其中一项功能是允许用户编辑表中的值,当单击运行时,它将使用用户输入作为函数的值,然后更新同一个表中的结果.下面是当前表和预期表的示例。因此,在第一个表中,如果用户更改通道 A 和 C 的电流值并单击运行,它应该将自身更新为表 预期输出中反映的值。

所以我的问题是,是否可以将可编辑的 DT 值作为函数的输入。

library(shiny)
library(DT)
shinyApp(
  ui = fluidPage(
    DT::dataTableOutput("x1"),
    actionButton("opt_run", "Run"),
    tags$h1("Expected Output"),
    DT::dataTableOutput("x2")
  ),
  server = function(input, output, session) {

    df <- data.table(Channel = c("A", "B","C"),
                     Current = c("2000", "3000","4000"),
                     Modified = c("2500", "3500","3000"),
                     New_Membership = c("450", "650","700"))

    output$x1 = renderDT(df, selection = 'none', editable = TRUE)

    expdf <- data.table(Channel = c("A", "B","C"),
                     Current = c("3000", "3000","5000"),
                     Modified = c("3500", "3500","6000"),
                     New_Membership = c("650", "650","1100"))

    output$x2 = renderDT(expdf, selection = 'none', editable = TRUE)

    })
  }
)

【问题讨论】:

    标签: r shiny dt


    【解决方案1】:

    我不确定您是否希望将它们全局存储。我会给你一个全球版本,这样你就可以把它保存到某个地方,一个数据库或磁盘上:

    您可以使用input$x1_cell_edit 访问单元格值,注意我按F5 刷新页面以检查值是否已保存

    library(shiny)
    library(DT)
    options(stringsAsFactors = F)
    
    df <- data.frame(Channel = c("A", "B","C"),
                     Current = c("2000", "3000","4000"),
                     Modified = c("2500", "3500","3000"),
                     New_Membership = c("450", "650","700"))
    
    expdf <- data.frame(Channel = c("A", "B","C"),
                        Current = c("3000", "3000","5000"),
                        Modified = c("3500", "3500","6000"),
                        New_Membership = c("650", "650","1100"))
    
    shinyApp(
      ui = fluidPage(
        DT::dataTableOutput("x1"),
        tags$h1("Expected Output"),
        DT::dataTableOutput("x2")
      ),
      server = function(input, output, session) {
    
        output$x1 = renderDT(df, selection = 'none', editable = TRUE)
    
        observeEvent(input$x1_cell_edit, {
          df[input$x1_cell_edit$row,input$x1_cell_edit$col] <<- input$x1_cell_edit$value
        })
    
        output$x2 = renderDT(expdf, selection = 'none', editable = TRUE)
        observeEvent(input$x2_cell_edit, {
          expdf[input$x2_cell_edit$row,input$x2_cell_edit$col] <<- input$x2_cell_edit$value
        })
      })
    }
    )
    

    【讨论】:

      猜你喜欢
      • 2022-01-08
      • 2019-01-29
      • 2021-11-19
      • 2019-10-25
      • 2016-05-27
      • 2020-12-29
      • 2018-11-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多