使用 R Shiny 中的 actionButton 附加行更新新的 table

Update new table with rows appended using actionButton in R Shiny

我在 Aurora 中有一个预报 table,我可以使用反应式读取和过滤加载。我正在使用操作按钮 'write_to_forecast_table' 将新行写入 table,但我无法同时在主 table 中加载新添加的行。

tabPanel("Plant-Screen",
         fluidRow(id='forecast_section',
                  column(width = 3, class='div-box-shadow',
                         tags$div(selectInput("fb_plant_input", label = 'Select Plant: ', choices= c(" All"), selected = NULL, multiple = FALSE, width = "100%"),
                                  
                                  selectInput("fb_material_input", label = 'Commodity', choices = c(" All"), multiple=FALSE, width="100%"),
                                  
                                  selectInput("fb_for_month_year", label = 'Procured for Month_Year', choices = c(" All"), multiple=FALSE, width="100%"),
                                  br(),
                                  tags$div(shinyWidgets::actionBttn(inputId = 'view_existing_forecasts', label='View Volume Forecast')),
                                  
                                  tags$div(shinyWidgets::actionBttn(inputId = 'create_new_forecast', label='Create New Forecast')
                                  ),
                                  
                                  bsModal(id="modal_demand_view", 
                                          title = "Create Volume Demand Forecast", 
                                          trigger = "create_new_forecast", 
                                          size="medium",
                                          fluidRow(id = 'new_demand_form',
                                                   column(width = 12,
                                                          tags$div(style="display:contents;",
                                                                   selectizeInput("selected_plant", 
                                                                                  label = 'Plant', 
                                                                                  options = list(placeholder = 'Select Plant'),
                                                                                  choices= unique(fb_plant_table$plant_name),
                                                                                  multiple=FALSE, 
                                                                                  width="100%"),
                                                                   br(),
                                                                   
                                                                   selectizeInput("selected_material",
                                                                                  label = 'Commodity',
                                                                                  options = list(placeholder = 'Select Commodity'),
                                                                                  choices = unique(fb_material_table$commodity),
                                                                                  multiple=FALSE,
                                                                                  width="100%"),
                                                                   
                                                                   br(),
                                                                   
                                                                   airDatepickerInput("selected_month_year",
                                                                                      label = "Enter for Month-Year :",
                                                                                      value = lubridate::ymd(today()),
                                                                                      minDate = lubridate::ymd(today()) %m+% months(1),
                                                                                      maxDate = lubridate::ymd(today()) %m+% months(12),
                                                                                      view = "months",
                                                                                      minView = "months",
                                                                                      dateFormat = "yyyy-mm",
                                                                                      width = "50%",
                                                                                      autoClose = TRUE
                                                                                      
                                                                   ),
                                                                   br(),
                                                                   numericInput("volume_requested",
                                                                                label = "Enter Additional Volume",
                                                                                value = 0,
                                                                                width = "50%"),
                                                                   br(),
                                                                   selectizeInput("volume_unit", label = "Unit of Volume",
                                                                                  choices = c("pounds"),
                                                                                  multiple = FALSE,
                                                                                  width = "50%"),
                                                                   br(),
                                                                   selectInput("selected_supplier", label = "Supplier",
                                                                               choices = "",
                                                                               multiple = FALSE,
                                                                               width = "50%")
                                                          )
                                                   )
                                          ),
                                          
                                          br(),
                                          tags$hr(),
                                          
                                          tags$div(shinyWidgets::actionBttn(inputId = 'write_to_forecast_table',
                                                                            label='New Volume Demand Created',
                                                                            color="success",
                                                                            style="material-flat")
                                          )
                                  )
                         )
                  ),
                  
                  column(width = 9, class='div-box-shadow',
                         tags$div(tags$h4 ('Volume Forecast Tabular View', 
                                           style="font-weight:bold;color: #ffd207;text-align: center;"
                                  ),
                                  br(),
                                  dataTableOutput("vol_forecast_meta_data") %>% shinycssloaders::withSpinner(color="#78620e")
                         )

服务器端:

#reading table from Aurora:
c <- dcon_iam()
vol_forecast_aurora <- DBI::dbGetQuery(c, 'select * from database_name.vol_forecast_aurora')
vol_forecast_aurora <- vol_forecast_aurora %>% group_by(plant_name, commodity, for_month_year) %>% mutate(cum_sum = cumsum(additional_volume))

DBI::dbDisconnect(c)

#Viewing forecast based on inputs selected from Select Input:
observeEvent(input$view_existing_forecasts, {
  
  view_fc_reactive <- reactive({
    vol_forecast_aurora %>%
      filter(plant_name == input$fb_plant_input) %>% 
      filter(commodity == input$fb_material_input) %>%
      filter(for_month_year == input$fb_for_month_year)
  })
  
  output$vol_forecast_meta_data <- DT::renderDataTable(view_fc_reactive(),
                                                       options = list(paging = FALSE, searching = FALSE),
                                                       rownames = FALSE)
})


#update table based on the new row added using Modal:
observeEvent(input$write_to_forecast_table, {
  
  forecast_temp <- z$vol_forecast_aurora
  
  forecast_temp$entered_by=input$user_id
  forecast_temp$entered_on=lubridate::ymd(today())
  forecast_temp$plant_name=input$selected_plant
  forecast_temp$commodity=input$selected_material
  forecast_temp$for_month_year=input$selected_month_year
  forecast_temp$additional_volume=input$volume_requested
  forecast_temp$unit_of_vol=input$volume_unit
  forecast_temp$supplier=input$selected_supplier
  
  
  forecast_temp = forecast_temp[, c('entered_by', 'entered_on', 'plant_name', 'commodity', 'for_month_year', 'additional_volume', 'unit_of_vol', 'supplier')]
  
  c = dcon_iam()
  write_to_caspian_aurora(c,
                          value= z$forecast_temp,
                          name="vol_forecast_aurora",
                          append = TRUE,
                          overwrite=FALSE,
                          row.names=FALSE
  )
  
  #removeModal('modal_demand_view')
  showNotification({"Demand Forecast Submitted"})
  DBI::dbDisconnect(c)
  
  
  #reloading the data to the app:
  c <- dcon_iam()
  
  vol_forecast_aurora <- DBI::dbGetQuery(c, 'select * from spendanalytics_ico.vol_forecast_aurora')
  vol_forecast_aurora <- vol_forecast_aurora %>% group_by(plant_name, commodity, for_month_year) %>% mutate(cum_sum = cumsum(additional_volume))
  
  DBI::dbDisconnect(c)
})

我需要帮助对服务器的最后一部分进行排序:observeEvent(input$write_to_forecast_table, {})

你的问题是基础数据 vol_forecast_aurora 只在应用程序加载时获取一次(并且它不是反应性的,因此 observeEvent(input$write_to_forecast_table 中的最后几行只创建一个本地对象在观察者内)。因此,当您在应用程序中更新数据库时,您看不到更改。我建议您将 vol_forecast_aurora 存储在 reactiveValues 对象中,以便您可以轻松更新它。

未经测试的代码:

#reading table from Aurora:
c <- dcon_iam()
data <- reactiveValues(vol_forecast_aurora = DBI::dbGetQuery(c, 'select * from database_name.vol_forecast_aurora') %>% group_by(plant_name, commodity, for_month_year) %>% mutate(cum_sum = cumsum(additional_volume)))

DBI::dbDisconnect(c)

#Viewing forecast based on inputs selected from Select Input:
observeEvent(input$view_existing_forecasts, {
  
  view_fc_reactive <- reactive({
    data$vol_forecast_aurora %>%
      filter(plant_name == input$fb_plant_input) %>% 
      filter(commodity == input$fb_material_input) %>%
      filter(for_month_year == input$fb_for_month_year)
  })
  
  output$vol_forecast_meta_data <- DT::renderDataTable(view_fc_reactive(),
                                                       options = list(paging = FALSE, searching = FALSE),
                                                       rownames = FALSE)
})


#update table based on the new row added using Modal:
observeEvent(input$write_to_forecast_table, {
  
  forecast_temp <- z$vol_forecast_aurora
  
  forecast_temp$entered_by=input$user_id
  forecast_temp$entered_on=lubridate::ymd(today())
  forecast_temp$plant_name=input$selected_plant
  forecast_temp$commodity=input$selected_material
  forecast_temp$for_month_year=input$selected_month_year
  forecast_temp$additional_volume=input$volume_requested
  forecast_temp$unit_of_vol=input$volume_unit
  forecast_temp$supplier=input$selected_supplier
  
  
  forecast_temp = forecast_temp[, c('entered_by', 'entered_on', 'plant_name', 'commodity', 'for_month_year', 'additional_volume', 'unit_of_vol', 'supplier')]
  
  c = dcon_iam()
  write_to_caspian_aurora(c,
                          value= z$forecast_temp,
                          name="vol_forecast_aurora",
                          append = TRUE,
                          overwrite=FALSE,
                          row.names=FALSE
  )
  
  #removeModal('modal_demand_view')
  showNotification({"Demand Forecast Submitted"})
  DBI::dbDisconnect(c)
  
  
  #reloading the data to the app:
  c <- dcon_iam()
  
  vol_forecast_aurora_local <- DBI::dbGetQuery(c, 'select * from spendanalytics_ico.vol_forecast_aurora')
  data$vol_forecast_aurora <- vol_forecast_aurora_local %>% group_by(plant_name, commodity, for_month_year) %>% mutate(cum_sum = cumsum(additional_volume))
  
  DBI::dbDisconnect(c)
})