如何更新闪亮的条件选择输入?

时间:2017-09-04 12:41:53

标签: r shiny

我的闪亮应用程序中有一些条件SelectInput面板,如:

ui <- fluidPage(

    sidebarPanel(
      selectInput("platform","Select Platform", choices = c("2.X MW","3.X MW", "4.X MW")),
                  conditionalPanel(
                    condition = "input.platform == '2.X MW'",
                    selectInput(
                      "model2","Select turbine", c("2.475m114")
                    )
                  ),
                  conditionalPanel(
                   condition = "input.platform == '3.X MW'",
                   selectInput(
                     "model3","Select turbine", c("3.0m114")
                    )
                 ),
                  conditionalPanel(
                    condition = "input.platform == '4.X MW'",
                    selectInput(
                     "model4","Select turbine", c("4.0m140")
                   )
               )

    ),

  mainPanel(
    DT::dataTableOutput("tbl")

))

server <- function(input, output, session) {
  coo <-reactive({

    conn <- dbConnect(
      drv = RMySQL::MySQL(),
      dbname = "database",
      host = "host",
      username = "user",
      password = "password")
    on.exit(dbDisconnect(conn), add = TRUE)
    if(input$platform=="2.X MW"){
    tab <- as.data.frame(dbReadTable(conn,paste0('`',input$model2,'`')))}
    if(input$platform=="3.X MW"){
      tab <- as.data.frame(dbReadTable(conn,paste0('`',input$model3,'`')))}
    if(input$platform=="4.X MW"){
      tab <- as.data.frame(dbReadTable(conn,paste0('`',input$model4,'`')))}
    ch=dbListTables(conn)
    ch21=ch[which(substring(ch,1,1)==2)]
    ch22=ch[which(substring(ch,1,1)=="m")]
    ch2=c(ch21,ch22)

    ch3=ch[which(substring(ch,1,1)==3)]

    ch4=ch[which(substring(ch,1,1)==4)]


    result <- list(tab=tab,ch2=ch2,ch3=ch3,ch4=ch4)
    return(result)
  })
  observeEvent(coo(),{

    updateSelectInput(session, "model2",
                      choices = coo()$ch2)

  })
  observeEvent(coo(),{

    updateSelectInput(session, "model3",
                      choices = coo()$ch3)

  })
  observeEvent(coo(),{

    updateSelectInput(session, "model4",
                      choices = coo()$ch4)

  })
  output$tbl <- DT::renderDataTable({
    tab1=coo()$tab
    DT::datatable(tab1,rownames= FALSE,options=list(pageLength = 30,autoWidth = TRUE))

  })
}

shinyApp(ui=ui, server=server)

这样,我的选择输入会更新但我无法从下拉列表中选择任何内容!无论我选择了什么,第一项都会被选中!

有什么想法吗?错在哪里!!! 错误是因为条件面板还是从Mysql数据库读取数据? enter image description here

0 个答案:

没有答案