我已经在选择输入函数中从数据库中检索了值。 但是当我创建一个场景并输入其名称(我从数据库中检索到的值)时,它会追加到我的表中,但未反映在代码所示的可用场景中。当我再次运行我的应用程序时,它将反映在可用状态中场景,但是我希望在不重新运行应用程序的情况下可以在选择输入中使用它。这是我的代码。
library("shiny")
library("shinydashboard")
library("pool")
library(DBI)
pool <- dbPool(drv = RMySQL::MySQL(),dbname = "demo",host = "db.cr7lk1jwjdht.us-east-2.rds.amazonaws.com",username = "kartik",password = "12345678", port = 3306)
mychoices = dbGetQuery(pool,"select availbale_scenario from sc;")
#mychoices = as.character(mychoices)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
actionButton("create_scenario", "Create Scenario"),
actionButton("load_scenario","load scenario"),
selectInput('n', "available scenarios", choices = mychoices, multiple = TRUE),
verbatimTextOutput("selected")
),
mainPanel(
uiOutput('tabs'),
uiOutput("input"),
uiOutput("inputs"),
uiOutput("inputss")
)
)
)
server <- function(input, output,session) {
observeEvent(input$create_scenario,{
output$input <- renderUI({
mainPanel(
textInput("txtInput","Enter scenario name"),
textOutput("sname"),
actionButton("save","save_scenario")
)
})
output$sname <- renderText({
input$txtInput
})
observeEvent(input$save,{
dd <- data.frame(x = input$txtInput,row.names = FALSE)
# print(dd)
dbWriteTable(pool,"available_scenario",dd,append = TRUE)
})
})
output$tabs = renderUI({
if(!is.null(input$n)){
Tabs <- lapply(paste("tab name:", input$n, sep=" "), tabPanel)
do.call(tabsetPanel, Tabs)}
})
observeEvent(input$load_scenario,{
output$inputs <- renderUI({
dashboardPage(
dashboardHeader(title = "Basic dashboard"),
dashboardSidebar(),
dashboardBody(
# Boxes need to be put in a row (or column)
fluidRow(
box(plotOutput("plot1", height = 250)),
box(
title = "Controls",
sliderInput("slider", "Number of observations:", 1, 100, 50)
))))
} )
histdata <- rnorm(500)
output$plot1 <- renderPlot({
data <- histdata[seq_len(input$slider)]
hist(data)
})
})
}
shinyApp(ui, server)