3
votes

I'm pulling a list from a DB and trying to update that list based on search results in Shiny. I've referred to a few answers here on SO but I haven't been able to get it to work correctly. Here's my code:

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())

  })

})

What am I missing here?

2

2 Answers

4
votes

The problem turned out to be moot as RStudio has added functionality for updating select lists with the function updateSelectInput. That's what I used, here is the code for future reference:

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())
  })

})
0
votes

I'd try to use a dynamic UI. I would not know if this is gonna work because I have no way to replicate it, but try it out and let me know what happens

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()))
    })

})

NOTE:The dropdown won't show anything if you do not click on the search button. But with some conditionals you would be able to show the full list if the user has not clicked on it.