更新下拉列表Shiny

时间:2015-05-21 18:03:12

标签: r drop-down-menu shiny

我从数据库中提取列表并尝试根据Shiny中的搜索结果更新该列表。我在这里提到了一些答案,但我还没有能够让它正常工作。这是我的代码:

ui.R

library(shiny)
library(RODBC)
library(plyr)
library(magrittr)

cxn <- odbcConnect("DSN", uid = "myID", pwd = "myPWD")

showList <- sqlQuery(cxn, "my query string", believeNRows=FALSE) %>% arrange(., SERIES_NAME) %>% .[ ,1] %>% as.character(.)

shinyUI(fluidPage(

  # Application title
  titlePanel("Select Show Data"),

  # Sidebar with a slider input for number of bins
  sidebarLayout(
    sidebarPanel(

      textInput("nameSearch", "Search by show name"),

      br(),

      submitButton("Search"),

      br(),

      selectInput("showDrop", "Select Show", showList)

    ),

    # Show a plot of the generated distribution
    mainPanel(

    )
  )
))

server.R

 library(shiny)

shinyServer(function(input, output, session) {

  searchResults <- reactive({
    showList[grepl(input$nameSearch, showList, ignore.case = TRUE)] 
  })

  observeEvent(input$Search, function() {

    output$searchResults <- renderTable({
      searchResults()  
    })

    updateTextInput(session, "showDrop", "Select Show", searchResults())

  })

})

我在这里缺少什么?

2 个答案:

答案 0 :(得分:2)

由于RStudio添加了使用函数updateSelectInput更新选择列表的功能,因此问题变得没有实际意义。这就是我使用的,这是未来参考的代码:

<强> ui.R

library(shiny)
library(RODBC)
library(plyr)
library(magrittr)

#cxn <- odbcConnect("DSN", uid = "myName", pwd = "myPwd")

showList <- sqlQuery(cxn, "my query string", believeNRows=FALSE) %>% arrange(., SERIES_NAME) %>% .[ ,1] %>% as.character(.)

shinyUI(fluidPage(

  # Application title
  titlePanel("Select Show Data"),

  # Sidebar with a slider input for number of bins
  sidebarLayout(
    sidebarPanel(

      textInput("nameSearch", "Search by show name"),

      br(),

      submitButton("Search"),

      br(),

      selectInput("showDrop", "Select show", choices = showList),

    ),
  )
))

<强> server.R

shinyServer(function(input, output, session) {

  searchResults <- reactive({
    showList[grepl(input$nameSearch, showList, ignore.case = TRUE)] 
  })

  observe({
    updateSelectInput(session, "showDrop", label = "Select show", choices =   searchResults())
  })

})

答案 1 :(得分:0)

我尝试使用动态UI。我不知道这是否会奏效,因为我无法复制它,但尝试一下让我知道会发生什么

ui.R

library(shiny)
library(RODBC)
library(plyr)
library(magrittr)

cxn <- odbcConnect("DSN", uid = "myID", pwd = "myPWD")

showList <- sqlQuery(cxn, "my query string", believeNRows=FALSE) %>% arrange(., SERIES_NAME) %>% .[ ,1] %>% as.character(.)

shinyUI(fluidPage(

  # Application title
  titlePanel("Select Show Data"),

  # Sidebar with a slider input for number of bins
  sidebarLayout(
    sidebarPanel(

      textInput("nameSearch", "Search by show name"),

      br(),

      actionButton("Search"),

      br(),

      uiOutput("dynamicDropdown")

    ),

    # Show a plot of the generated distribution
    mainPanel(

    )
  )
))

server.R

library(shiny)

cxn <- odbcConnect("DSN", uid = "myID", pwd = "myPWD")

showList <- sqlQuery(cxn, "my query string", believeNRows=FALSE) %>% arrange(., SERIES_NAME) %>% .[ ,1] %>% as.character(.)

shinyServer(function(input, output, session) {

  searchResults <- reactive({
     if(input$Search == 0) return(NULL)

    isolate(showList[grepl(input$nameSearch, showList, ignore.case = TRUE)])
  })

    output$dynamicDropdown <- renderUI({
     if(input$Search == 0) return(NULL)
     isolate(selectInput("showDrop", "Select Show", searchResults()))
    })

})

注意:如果您未单击搜索按钮,则下拉列表将不会显示任何内容。但是对于某些条件,如果用户没有点击它,您将能够显示完整列表。