使用已编辑 table 中的值在 Shiny 中进行计算

Use values from edited table for calculations in Shiny

我正在尝试做的是允许用户将 configuration/lookup excel table 传入 shiny,在 shiny 中显示此 table,允许用户在 shiny 中编辑单元格,并使用从 editable table 编辑的值进行计算。我的问题出现在最后一步 "use the values that were edited from the editable table for calculations".

excel 文件由 2 个选项卡组成,其中包含以下内容的数据:

选项卡 1 名称:"parameters" data.frame(名称 = c("a", "b", "c"), 值 = c(1:3))

选项卡 2 名称:"parameters2" data.frame(名称 = c("a", "b", "c"), 值 = c(4:6))

理想的闪亮应用会执行以下操作:

1) 上传时,执行计算,将选项卡 1 和选项卡 2 的第一个未更改的值相加。这将是 1 + 4 = 5。

2) 如果用户将选项卡 1 的值 1 编辑为 8,则计算结果为 8 + 4 = 12。

实际上,如果用户对其进行任何编辑,我想使用编辑后的 ​​tables 值来更新我的所有计算。我知道这可以通过简单地上传一个闪亮的新文件来完成,但我宁愿让他们以闪亮的方式执行此操作,而不是上传一个新文件。

这是我闪亮的应用程序。感谢任何 help/guidance!

    library(shiny)
library(DT)
shinyApp(
  ui <- fluidPage(
    fileInput(inputId = "config", label = "Upload Configuration File", 
              multiple = F, accept = c(".xlsx", ".xls")), 
    verbatimTextOutput("txt"), 
    tagList(tags$head(tags$style(type = 'text/css','.navbar-brand{display:none;}')),
            navbarPage(title = "",
                       tabPanel(title = "Parameters",
                                dataTableOutput(outputId = "edit.param", width = 2)), 
                       tabPanel(title = "Parameters2",
                                dataTableOutput(outputId = "edit.param2", width = 2))
            )
    )
  ),
  server = function(input, output, session) {

    config.path = reactive({

      inFile = input$config

      if(is.null(inFile)) {
        return(NULL)
      } else {
        return(inFile$datapath)
      }

    })

    df.param = reactive({
      read_excel(path = config.path(), sheet = "parameters")
    })

    df.param2 = reactive({
      read_excel(path = config.path(), sheet = "parameters2")
    })

    output$edit.param = renderDT(df.param(), selection = "none", server = F, editable = "cell")
    output$edit.param2 = renderDT(df.param2(), selection = "none", server = F, editable = "cell")

    observeEvent(input$edit.param_cell_edit, {
      df.param()[input$edit.param_cell_edit$row, input$edit.param_cell_edit$col] <<- input$edit.param_cell_edit$value
    })

    observeEvent(input$edit.param2_cell_edit, {
      df.param()[input$edit.param2_cell_edit$row, input$edit.param2_cell_edit$col] <<- input$edit.param2_cell_edit$value
    })

    output$txt = reactive({

      df.param()$value[1] + df.param2()$value[1]

    })

  }

)

我也在服务器部分尝试过这个,但也没有成功:

    output$edit.param = renderDT(df.param(), selection = "none", server = F, editable = "cell")
output$edit.param2 = renderDT(df.param2(), selection = "none", server = F, editable = "cell")

observe(input$edit.param_cell_edit)
observe(input$edit.param2_cell_edit)

你能试试这个吗? (我没试过)

library(shiny)
library(DT)

shinyApp(
  ui <- fluidPage(
    fileInput(inputId = "config", label = "Upload Configuration File", 
              multiple = F, accept = c(".xlsx", ".xls")), 
    verbatimTextOutput("txt"), 
    tagList(tags$head(tags$style(type = 'text/css','.navbar-brand{display:none;}')),
            navbarPage(title = "",
                       tabPanel(title = "Parameters",
                                dataTableOutput(outputId = "edit_param", width = 2)), 
                       tabPanel(title = "Parameters2",
                                dataTableOutput(outputId = "edit_param2", width = 2))
            )
    )
  ),
  server = function(input, output, session) {

    config.path = reactive({

      inFile = input$config

      if(is.null(inFile)) {
        return(NULL)
      } else {
        return(inFile$datapath)
      }

    })

    df_param <- reactiveVal()
    observe({
      req(config.path())
      df_param(read_excel(path = config.path(), sheet = "parameters"))
    })

    df_param2 <- reactiveVal()
    observe({
      req(config.path())
      df_param2(read_excel(path = config.path(), sheet = "parameters2"))
    })

    output$edit_param = renderDT({
      req(df_param())
      datatable(isolate(df_param()), selection = "none", editable = "cell")
    })
    output$edit_param2 = renderDT({
      req(df_param2())
      datatable(isolate(df_param2()), selection = "none", editable = "cell")
    })

    proxy <- dataTableProxy("edit_param")
    proxy2 <- dataTableProxy("edit_param2")

    observeEvent(input$edit_param_cell_edit, {
      info <- input$edit_param_cell_edit
      df_param(editData(df_param(), info, proxy, resetPaging = FALSE))
    })

    observeEvent(input$edit_param2_cell_edit, {
      info <- input$edit_param2_cell_edit
      df_param2(editData(df_param2(), info, proxy2, resetPaging = FALSE))
    })

    output$txt = renderPrint({
      df_param()$value[1] + df_param2()$value[1]
    })

  }

)