0

I am having a lot of trouble getting a search filtering module working.

I am to run stats on a large database of cat owner information. I want my search module to bring up a list of possible owners(that the user can select from) based on a selection from a list of cat breeds. I thought wrapping the updateSelectInput with observe and using a reactive cat owner expression would facilitate this, in the module, but it is not working( and I can't guess why this is happening or how to debug this). It worked in these other posts([1]:R shiny passing reactive to selectInput choices , [2]:using values from a reactive input to directly input into a custom function)

Why won't my selectInput update with cat owners?

library(shiny)
df=data.frame(
  cat=c("tabby","DSH","MSH","LSH","DSH","MSH","LSH","sphinx"),
  owner=c("Foo","Bar","Bash","Foo","Foo","Foo","Bar","Bash"),stringsAsFactors = F)
refinedSearch<-function(input, output, session){
  ownsCat<-reactive({df[df$cat%in%input$cat,"owner"]})
  observe({updateSelectInput(session, "ownerSelected",
                             label ="Owned By",choices = ownsCat())})
  return()
}
refinedSearchUI<-function(id){
  ns <- NS(id)
  fluidRow(
    column(4,selectInput(ns("cat"),"Cat",selectize = T, 
                         choices =c("tabby","DSH","MSH","LSH","sphinx") )),
    column(4,selectInput(ns("ownerSelected"),"Owned By","",selectize = T))
  )
}
ui <- fluidPage(
  h1("Find cats owners"),
  fluidRow(column(10,offset=1, refinedSearchUI("tmp"))),
  fluidRow(column(10,offset=1, actionButton("addFilter","Add a Filter",
                                            icon = icon("plus"))))
)
server <- function(input, output,session) {
  refinedSearch(input,output,session)
  observeEvent(input$add, {insertUI(selector = "#addFilter",where = "beforeBegin",
                                    ui = refinedSearch(input,output,session))})
}
shinyApp(ui = ui, server = server)

Thank y'all for you time.

Community
  • 1
  • 1
sabeepa
  • 131
  • 1
  • 6

1 Answers1

1

There seems to be quite a bit of confusion on how to call modules. You need to use the callModule() function in the server. Also, when inserting UI (using the insertUI()function), you need to call the refinedSearchUI() function, not the refinedSearch() function (which, again, should always be called through callModule(), so it should never actually get called directly like that).

I'd recommend a re-reading of the modules article.

You also have a typo. The event in your observeEvent() function should be input$addFilter, not input$add (which doesn't exist, so that observer is never fired..)

If you change your server function to this, your app will work as expected:

server <- function(input, output,session) {
  callModule(refinedSearch, "tmp")

  observeEvent(input$addFilter, {
    id <- paste0("filter_", input$add)
    insertUI(selector = "#addFilter",where = "beforeBegin",
             ui = refinedSearchUI(id))
    callModule(refinedSearch, id)
  })
}
Bárbara Borges
  • 889
  • 7
  • 15