如何使用在renderUI的selectInput中选择的选项在主面板中生成不同的输出?

时间:2019-03-12 04:41:37

标签: r shiny rstudio

我能够使用renderUI动态生成不同的选择。现在,我希望使用选定的输入并在主面板上动态更改输出

selectInput包含不同数据库的列表。默认情况下,我使用最新的数据库,但我想为用户提供使用以前的数据库的选项。这些先前的数据库显示在selectInput下拉菜单中。如前所述,我可以显示所有不同的数据库,但是我不能选择一个数据库,也不能根据所选的选择更改mainPanel输出。

这是我的ui.R

shinyUI(fluidPage(

  # Application title
  titlePanel("Fill Traffic Analysis"),

  # Sidebar with a slider input for number of bins 
  sidebarLayout( 
    sidebarPanel(
      sliderInput("mkts",
                  "Percent of outside Fill traffic",
                  min = 0,
                  max = 100,
                  value = 60),
      helpText("The following list will be generated dynamically"),
      uiOutput("vx"),
      #downloadButton("downloadData", "Download"),
      width=2
    ),

    # Show a plot of the generated distribution 
    mainPanel(
      h1("Database"),
      h2(textOutput("database")),
      plotOutput("egressMkts.FillTraffic", width = "125%", height = 720)
    )
  )
)
)

这是我的服务器。R

shinyServer(
  function(input, output) {

  tryCatch({


    #Let's set the working directory to /data which is where the database files are stored
    setwd("/data")
    #Find the first 10 files with the string "router" in it
    files <- system("ls -lt | grep \"router\" | head", intern=TRUE)
    pos=regexpr('router2router', files[1])
    database <- substr(files[1],pos,nchar(files[1]))
    output$database <- renderText(database)

    output$vx <- renderUI({
      selectInput("databaseList","Select a database",choices=substr(files,pos,nchar(files)))
    })

    #database <- get(input$databaseList)

    #Connect to the SQL database
    db<-dbConnect(SQLite(),dbname=database)
    dbListTables(db)
    dbListFields(db,"netflow_table")

    #Query the database 
    query <- "select ingressGW, ingressCDN, ingressOnNet, egressGW, egressCDN, ipbusBytes from netflow_table where ingressCDN NOT LIKE 
\"%notCDN%\" and egressCDN NOT LIKE \"%notCDN%\" "
    raw.traffic.data <- dbGetQuery(db,query)

    .
    .
    .
    .
    .

    output$egressMkts.FillTraffic <- renderPlot({

      #database <- get(input$databaseList)
        .
        .
        .
        .
        .
        .

    })
  },
  error=function(e){
    cat(paste("in err handler\n"),e)
  },
  warning=function(w){
    cat(paste("in warn handler2\n"),w)
  }


  )

}
)

1 个答案:

答案 0 :(得分:0)

尝试使用switch

selcted_database = reactive(switch(input$databaseList,
                                "DB1" = name_database_1,
                                "DB2" = name_database_2,
                                ...))

M