DaniCee
DaniCee

Reputation: 3217

R Shiny: How to make "server-side" selectizeInput work with reactiveValues

Sorry in advance I am still a noob when it comes to R Shiny. I am trying to do a "server-side" selectizeInput() but I cannot make it work, even after going through https://shiny.rstudio.com/articles/selectize.html plenty of times...

I have my Shiny app that I built with "Package for Shiny app using Golem" in RStudio. It contains a few modules (created with golem::add_module()) that do different things, and I use reactive values to pass all the data and selections around the modules.

Sorry I find it hard to make a MWE out of this, the data is read in the main app_server.R script wrapped inside observe(), and passed in the r variable where r <- reactiveValues(query = reactiveValues())

In one of the modules I do selectizeInput, it works perfectly fine. It looks like this (with plenty other stuff I just omitted):

#' scatter_side UI Function
#'
#' @description A shiny Module.
#'
#' @param id,input,output,session Internal parameters for {shiny}.
#'
#' @noRd 
#'
#' @importFrom shiny NS tagList 
mod_scatter_side_ui <- function(id){
  
  ns <- NS(id)
  tagList(
    shinyjs::useShinyjs(),
    #.
    #.
    #.
    uiOutput(outputId = ns("selected_ids"))
  )
}
    
#' scatter_side Server Functions
#'
#' @noRd 
mod_scatter_side_server <- function(id, r){
  moduleServer( id, function(input, output, session){
    ns <- session$ns
    #.
    #.
    #.
    output$selected_ids <- renderUI({
      selectizeInput(inputId = ns("selected_ids"), label = "Highlight Points (IDs)",
                     choices = r$my_data_frame$ID,
                     selected = NULL,
                     multiple = TRUE)
    })

    # add selection to reactive values
    #.
    #.
    #.
    observeEvent(input$selected_ids, {
      r$selected_ids <- input$selected_ids
    }, ignoreNULL = FALSE)

  })
}
    
## To be copied in the UI
# mod_scatter_side_ui("scatter_side_ui_1")
    
## To be copied in the server
# mod_scatter_side_server("scatter_side_ui_1")

However, I get the following warning:

Warning: The select input "scatter_side_ui_1-selected_ids" contains a large number of options; consider using server-side selectize for massively improved performance. See the Details section of the ?selectizeInput help topic.

So I am trying to do this "server-side" selectizeInput(), but cannot make it work. The following is my best attempt (which does not work, no drop down menu with the selectize items, but at least does not crash):

#' scatter_side UI Function
#'
#' @description A shiny Module.
#'
#' @param id,input,output,session Internal parameters for {shiny}.
#'
#' @noRd 
#'
#' @importFrom shiny NS tagList 
mod_scatter_side_ui <- function(id){
  
  ns <- NS(id)
  tagList(
    shinyjs::useShinyjs(),
    #.
    #.
    #.
    selectizeInput(inputId = "selected_ids", label = "Highlight Points (IDs)",
                   choices = NULL,
                   multiple = TRUE),
  )
}
    
#' scatter_side Server Functions
#'
#' @noRd 
mod_scatter_side_server <- function(id, r){
  moduleServer( id, function(input, output, session){
    ns <- session$ns
    #.
    #.
    #.
    observe(updateSelectizeInput(
      session, 
      inputId = ns("selected_ids"), 
      choices = r$my_data_frame$ID,
      selected = NULL, server = TRUE
    ))

    # add selection to reactive values
    #.
    #.
    #.
    observeEvent(input$selected_ids, {
      r$selected_ids <- input$selected_ids
    }, ignoreNULL = FALSE)

  })
}
    
## To be copied in the UI
# mod_scatter_side_ui("scatter_side_ui_1")
    
## To be copied in the server
# mod_scatter_side_server("scatter_side_ui_1")

Any help? Thanks!

Upvotes: 1

Views: 510

Answers (0)

Related Questions