使用R Shiny中的actionButton更新带有附加行的新表



我在Aurora中有一个Forecast表,我可以使用reactive读取和过滤负载。我正在使用操作按钮"write_to_forecast _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(输入$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)
})

最新更新