简体   繁体   English

R Shiny应用-在DT中编辑值并更新

[英]R shiny app - Edit value in DT and update

I have a shiny app where one of the functionality is allowing the user to edit the values in the table and when clicked on run it will use the user inputs as the value to a function and then update the results in the same table. 我有一个闪亮的应用程序,其中的一项功能是允许用户编辑表中的值,当单击运行时,它将使用用户输入作为函数的值,然后更新同一表中的结果。 Below is the example of the current table and the expected table. 下面是当前表和预期表的示例。 So in the first table if the user changes the values for current for Channel A and C and click run it should update itself to the values reflected in table expected output . 因此,在第一个表中,如果用户更改通道A和C的电流值并单击运行,则它应该将自身更新为表预期输出中反映的值。

So my question is, is it possible to take editable DT values as an input to a function. 所以我的问题是,是否可以将可编辑的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)

    })
  }
)

I wasnt sure if you want them to be stored globally or not. 我不确定您是否要将它们存储在全局范围内。 I will give you a global version so you can save it somewhere, to a DB or on Disk: 我将为您提供一个全局版本,以便您可以将其保存到数据库或磁盘上的某个位置:

You can access the cell values using input$x1_cell_edit , note that I am pressing F5 to refresh the page to check if the values have been saved or not 您可以使用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
    })
  })
}
)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM