selectInput not updating getting updated in InserUI

Available 1 not getting updated because of diploma courses.
My data contains entries of education details. How to update one selectInput from other selectInput.

           if(msg=='Diploma')
           {
             insertUI(selector = "#message", where = "beforeBegin",
                      ui=div(class="chat-bubbles",
                             div(class="bubble",
                                 wellPanel(
                                   p("As per your selection you are eligible for:", tags$br(),
                                     selectInput("diploma_courses", "Disciplines", choices =c("All",unique(as.character(a1$`Discipline Category1`) )),
                                                 multiple = T)
                                     
                                   )))))
             
             observeEvent(input$diploma_courses,{
               insertUI(selector = "#message", where = "beforeBegin",
                        ui=div(class="chat-bubbles",
                               div(class="bubble",
                                   wellPanel(
                                     p(
                                       selectInput("available1", "Specialization", choices =c(as.character(a1$Subject1)[a1$`Discipline Category1`== input$diploma_courses])) )
                                   ))))
               
             })

Is this part of the server? What is msg? You know that if(msg=='Diploma') will only get evaluated once right, it is not a reactive expression.

Yes. It is part of the server. Msg is a variable I have created for input$message.
My code is like-

server<- function(input, output, session)
{
  
  
  # Declaring and Initializing Global Variables
  i <- 1
  lvl <- reactiveVal()
  lvl(i)

replyMessage<- function(lvl,msg)
  {
    message('Level:', lvl)
    message('Message:', msg)
    
    switch(lvl,
           
if(msg=='Diploma')
           {
             insertUI(selector = "#message", where = "beforeBegin",
                      ui=div(class="chat-bubbles",
                             div(class="bubble",
                                 wellPanel(
                                   p("As per your selection you are eligible for:", tags$br(),
                                     selectInput("diploma_courses", "Disciplines", choices =c("All",unique(as.character(a1$`Discipline Category1`) )),
                                                 multiple = T)
                                     
                                   )))))
             
             observeEvent(input$diploma_courses,{
               insertUI(selector = "#message", where = "beforeBegin",
                        ui=div(class="chat-bubbles",
                               div(class="bubble",
                                   wellPanel(
                                     p(
                                       selectInput("available1", "Specialization", choices =c(as.character(a1$Subject1)[a1$`Discipline Category1`== input$diploma_courses])) )
                                   ))))
               
             })

 getMessage<- function(lvl)
  {
    # Observer Event for Message Box
    observeEvent(input$send,{
      if(input$message == '')
      {
        insertUI(
          selector = "#message",
          where = "beforeBegin",
          ui=div(class="chat-bubbles",
                 div(class="bubble",p("Kindly provide a valid input."))
          )
        )
        clearInput()
      }
      else
      {
        replyMessage(lvl(),input$message)
      }
      
    })

That's not how shiny works. Take a look at some examples and see how they set up the reactive elements.

https://shiny.rstudio.com/articles/dynamic-ui.html

Anything in the server() will only be executed once. Shiny takes the reactive parts of the server and builds a network which controls the reaction and execution of the parts.

https://shiny.rstudio.com/articles/reactivity-overview.html

It says- " To update a part of the UI (ex: an input object), you must use the appropriate render function or a customized reactive function. Does it mean I have to replace my msg=='Diploma' with obserEvent?

In my code-
I have a action button named- 'diploma'. when I click on it, it shall show the list containing it's courses(diploma_courses) and when I select any of the courses(diploma_courses), I shall get another selectInput list of programmes(available1).
Now guide me, where I am going wrong.

I tried creating the other selectinput inside the observeEvent. It is working. But whenever I am changing the selectInput(diploma_courses), a new insertUI is opening for every selection I am doing. How to avoid this?

It's not clear what you're trying to do, so I'd recommend reading more about dynamic UI and creating a minimal reprex.

I want to filter the data based on selection of first select Input ( diploma). i.e if I select any diploma course, I shall get the filtered data in available1 select Input.

My code is-

ui<- fluidPage(useShinyjs(),
               #extendShinyjs(text = "shinyjs.button = function() {window.scrollTo(0, 50);}"),
               tags$head(tags$link(rel="stylesheet", href="style.css")),
               titlePanel("Chatbot"),
               fluidRow(column( width = 8,
                                div(id='outDiv',
                                   
                                    panel(style = "overflow-y:scroll; max-height: 300px; position:relative; align: centre",
                                          textInput("message", label = "",placeholder = "Type your message here."),
                                          actionButton("send", "Send"), heading = "Smart Advisor", status = "primary")
                                )
               )))


server<- function(input, output, session)
{
  
  
  # Declaring and Initializing Global Variables
  i <- 1
  lvl <- reactiveVal()
  lvl(i)

replyMessage<- function(lvl,msg)
  {
    message('Level:', lvl)
    message('Message:', msg)
    
    switch(lvl,
           
if(msg=='Diploma')
           {
             insertUI(selector = "#message", where = "beforeBegin",
                      ui=div(class="chat-bubbles",
                             div(class="bubble",
                                 wellPanel(
                                   p("As per your selection you are eligible for:", tags$br(),
                                     selectInput("diploma_courses", "Disciplines", choices =c("All",unique(as.character(a1$`Discipline Category1`) )),
                                                 multiple = T)
                                     
                                   )))))
             
             observeEvent(input$diploma_courses,{
               insertUI(selector = "#message", where = "beforeBegin",
                        ui=div(class="chat-bubbles",
                               div(class="bubble",
                                   wellPanel(
                                     p(
                                       selectInput("available1", "Specialization", choices =c(as.character(a1$Subject1)[a1$`Discipline Category1`== input$diploma_courses])) )
                                   ))))
               
             })

 getMessage<- function(lvl)
  {
    # Observer Event for Message Box
    observeEvent(input$send,{
      if(input$message == '')
      {
        insertUI(
          selector = "#message",
          where = "beforeBegin",
          ui=div(class="chat-bubbles",
                 div(class="bubble",p("Kindly provide a valid input."))
          )
        )
        clearInput()
      }
      else
      {
        replyMessage(lvl(),input$message)
      }
      
    })
Solution

That's not a minimal reprex because you've forgotten to load any packages, and you haven't made any effort to eliminate code that's unrelated to the problem you're having.

library(shiny)
library(shinydashboardPlus)
library(shinydashboard)
library(shinyjs)
library(shinyWidgets)
library(tidyverse)
library(dplyr)
library(excelR)
library(readxl)
library(readr)

a<- read_excel("cities.xlsx")

ui<- fluidPage(
  titlePanel("Country Details"),
  fluidRow(column( width = 8,
                   div( 
                     #textInput("send", label = "",placeholder = "Type your send here."),
                     actionButton("send", "Send")
                   )
  )))

server<- function ( input, output, session)
{
  
  observeEvent(input$send, {
    insertUI(selector="#send", where = "beforeBegin",
             ui=div(class="bubble",
                    wellPanel(
                      p( selectInput("country", "Country", choices =c("All",unique(as.character(a$Country) )), multiple = T) 
                         ))))

  })
observeEvent(input$country,{
  insertUI(selector = "#send", where = "beforeBegin",
           ui=div( div(class="bubble",
                       wellPanel(
                         p(
                           selectInput("state", "State", choices =c(unique(as.character(a$State))[a$Country==input$country])) )
                       ))))
  
})

observeEvent(input$state,{
  insertUI(selector = "#send", where = "beforeBegin",
           ui=div( div(class="bubble",
                       wellPanel(
                         p(
                           selectInput("city", "City", choices =c(unique(as.character(a$city))[a$Country==input$country && a$state==input$state])) )
                       ))))
  
})

} 
shinyApp ( ui, server)

There's an error in these lines. Brackets in the wrong place and wrong case.

selectInput("state", "State", choices = unique(as.character(a$State[a$Country==input$country])) )

selectInput("city", "City", choices = unique(as.character(a$city[a$Country==input$country && a$state==input$State])) )
1 Like