Server Side Selectize is not working in RShiny app

Here is the main / only docs on server-side selectize from RStudio. Following the example in this article, I have created the example below. The main goal of the code below is to render the input as a server-size selectize input. Note that the fetching of options for the select happens globally near the top of our app.R file - we fetch data for the named character vector namelist_nba, which we use as the value for choices. For reproducibility, I have commented out our data fetching code & have hard-coded the top 20 responses:

app.R - you should be able to run this.


# Fetch Options for Selectize Input

# source('scripts/pingDB.R')
# sql__namelist_nba <<- paste(readLines("sql/nba/namelist_nba.txt"), collapse=" ")
# namelist_nba <<- getData(sql__namelist_nba)
# namelist_nba <<- namelist_nba[order(namelist_nba$full_name), ]
# namelist_nba <<- setNames(namelist_nba$player_id, namelist_nba$full_name)

# hardcode first 20 results for stackoverflow post
namelist_nba <- c(`A.C. Green` = 920L, `A.J. Bramlett` = 1920L, `A.J. Davis` = 203667L, 
  `A.J. Guyton` = 2062L, `Aaron Best` = 1628700L, `Aaron Brooks` = 201166L, 
  `Aaron Craft` = 203905L, `Aaron Epps` = 1629200L, `Aaron Gordon` = 203932L, 
  `Aaron Gray` = 201189L, `Aaron Harrison` = 1626151L, `Aaron Holiday` = 1628988L, 
  `Aaron Jackson` = 1628935L, `Aaron Johnson` = 203638L, `Aaron McKie` = 243L, 
  `Aaron Miles` = 101223L, `Aaron Nesmith` = 1630174L, `Aaron Pettway` = 202925L, 
  `Aaron Thomas` = 1628045L, `Aaron White` = 1626206L)


# create body and sidebar
ui_body <- dashboardBody()

# note use of NS() and modules
ns2 <- NS('nba_player_profile')
ui_sidebar <- dashboardSidebar(
  
  sidebarMenu(
      id = "sidebarMenu",
      menuItem("These Pages", tabName = "team",
        menuSubItem("Player Profile", tabName = "player_profile_nba"),
        conditionalPanel(
          "input.sidebarMenu === 'player_profile_nba'",
          class = NULL,
          selectizeInput(inputId = ns2("player_input"), label = 'Player Search: ', choices = NULL)
        )
      )
  )
)

# server module for "player profile" page
server__player_profile <- function(id) {
  moduleServer(
    id,
    function(input, output, session) {
      updateSelectizeInput(
        session, 
        inputId = session$ns('player_input'), 
        choices = namelist_nba,
        selected = namelist_nba[1],
        server = TRUE)
    }
  )
}

# shinyserver is where we combine all of our server modules...
server <- shinyServer(function(input, output, session) {
  observeEvent(input$sidebarMenu, {
    print(paste0("sidebarMenu tabName: ", input$sidebarMenu))
  })

  server__player_profile(id = 'nba_player_profile')
})

# ui is where we combine header,sidebar,body
ui <- dashboardPage(
  title="Dashboard Title",
  dashboardHeader(
    title = div("Our Databoard header title"), 
    titleWidth = 300),
  ui_sidebar, ui_body
)

# and return / run the app
app <- shinyApp(ui = ui, server = server)
runApp(app)

When this app is run, the selectize input currently does not work at all. I cannot type in player names, and clicking on the box shows no dropdown (although it does show a tiny, empty dropdown that flickers for a fraction of a second). How can we update our code so that server-side selectize works here?

Note that our code example is made a bit more complex by our use of shiny modules, which is all documented here.

library(shiny)
library(shinydashboard)
# Fetch Options for Selectize Input

# source('scripts/pingDB.R')
# sql__namelist_nba <<- paste(readLines("sql/nba/namelist_nba.txt"), collapse=" ")
# namelist_nba <<- getData(sql__namelist_nba)
# namelist_nba <<- namelist_nba[order(namelist_nba$full_name), ]
# namelist_nba <<- setNames(namelist_nba$player_id, namelist_nba$full_name)

# hardcode first 20 results for stackoverflow post
namelist_nba <- c(`A.C. Green` = 920L, `A.J. Bramlett` = 1920L, `A.J. Davis` = 203667L, 
                  `A.J. Guyton` = 2062L, `Aaron Best` = 1628700L, `Aaron Brooks` = 201166L, 
                  `Aaron Craft` = 203905L, `Aaron Epps` = 1629200L, `Aaron Gordon` = 203932L, 
                  `Aaron Gray` = 201189L, `Aaron Harrison` = 1626151L, `Aaron Holiday` = 1628988L, 
                  `Aaron Jackson` = 1628935L, `Aaron Johnson` = 203638L, `Aaron McKie` = 243L, 
                  `Aaron Miles` = 101223L, `Aaron Nesmith` = 1630174L, `Aaron Pettway` = 202925L, 
                  `Aaron Thomas` = 1628045L, `Aaron White` = 1626206L)


# create body and sidebar
ui_body <- dashboardBody()

ui_player_profile <- function(id) {
  ns <- NS(id)
  tagList(
    selectizeInput(inputId = ns("player_input"), 
                   label = 'Player Search: ', choices = NULL)
  )
}

ui_sidebar <- dashboardSidebar(
  
  sidebarMenu(
    id = "sidebarMenu",
    menuItem("These Pages", tabName = "team",
             menuSubItem("Player Profile", tabName = "player_profile_nba"),
             conditionalPanel(
               "input.sidebarMenu === 'player_profile_nba'",
               class = NULL,
               ui_player_profile("nba_player_profile")
             )
    )
  )
)




# server module for "player profile" page
server__player_profile <- function(id) {
  moduleServer(
    id,
    function(input, output, session) {
      updateSelectizeInput(
        session, 
        inputId = 'player_input', 
        choices = namelist_nba,
        selected = namelist_nba[1],
        server = TRUE)
    }
  )
}

# shinyserver is where we combine all of our server modules...
server <- shinyServer(function(input, output, session) {
  observeEvent(input$sidebarMenu, {
    print(paste0("sidebarMenu tabName: ", input$sidebarMenu))
  })
  
  server__player_profile(id = 'nba_player_profile')
})

# ui is where we combine header,sidebar,body
ui <- dashboardPage(
  title="Dashboard Title",
  dashboardHeader(
    title = div("Our Databoard header title"), 
    titleWidth = 300),
  ui_sidebar, ui_body
)

# and return / run the app
app <- shinyApp(ui = ui, server = server)
runApp(app)
1 Like

This did it for me. Thanks so much!

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.