R noob
R noob

Reputation: 513

Passing reactive data as choices for updateSelectizeInput

I am trying to pass reactive data to updateSelectizeInput but instead get the following error :

Error in .getReactiveEnvironment()$currentContext() : Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)

My code is as follows :

ui.R

 library(shiny)


  ui_panel <- 
  tabPanel("Text Input Test",
          sidebarLayout(
           sidebarPanel( 


           selectizeInput('Organisations', label = "Organisation Search", 
   choices = NULL, options = list(
             placeholder = 'Type the organisation name', maxOptions = 10, 
   maxItems = 1, searchConjunction = 'and')),
           tags$style(type="text/css",
                      ".selectize-input::after{visibility:hidden;};"
                      ),


           br(),
           selectInput(
             inputId="selectData",
             label=" ",
             choices=c("Universities", "Hospitals")
           ),

           br()
         ),
         mainPanel(
           tabsetPanel(

             tabPanel("output of Organisation search 
 details",htmlOutput("orgsearchdetails"))

           )
         )
       ))


ui <- shinyUI(navbarPage(" ",ui_panel))

server.R

   library(shiny)


  shinyServer(
   function(input, output, session) {



    orgdata <- reactive({if(input$selectData=='Universities'){
    orgdata <- read.csv("universities.csv")
   }else if (input$selectData=='Hospitals'){ 
    orgdata <- read.csv("hospitals.csv")
    }
    orgdata
   })




    updateSelectizeInput(session, 'Organisations', choices = 
   as.character(unique(
     orgdata()$name)), server = TRUE)




  output$orgsearchdetails <-renderUI({ 


    })


  session$onSessionEnded(function() { dbDisconnect(con) })
  })

Is there a way I can pass the reactivity of the selectInput to the updateSelectizeInput ?

I am opting for updateSelectizeInput instead of a reactive selectInput so as to avoid populating the filed at the launch of the application.

Any help or insight is highly welcome. thank you in advance.

Upvotes: 6

Views: 1970

Answers (1)

Scratch&#39;N&#39;Purr
Scratch&#39;N&#39;Purr

Reputation: 10409

I'm not much of a shiny expert, but from the dashboards I've been building, here is what I would do. I would create a reactiveValues variable called selectedData. Then in my observeEvent, I would pull the csv and update the selectedData. The reason why I chose to use reactiveValues in my app is that they can be stateful rather than just always reacting to some input, and I can call them in other areas of the app that requires the same information. As for using observeEvent, it's simple because you can then wrap your updateSelectizeInput within the observer.

shinyServer(
  function(input, output, session) {
    # init reactiveValues
    selectedData <- reactiveValues()

    observeEvent(input$selectData, {
      if (input$selectData == "Universities") {
        selectedData$orgdata <- read.csv("universities.csv")
      } else if (input$selectData == "Hospitals") {
        selectedData$orgdata <- read.csv("hospitals.csv")
      }

      updateSelectizeInput(session, "Organisations",
                           choices = as.character(unique(selectedData$orgdata$name)),
                           server = TRUE)
    })

    ... # rest of your code

  }
)

Upvotes: 4

Related Questions