I am pretty new to R and was trying to build a small utility just for my learning.
I have 3 fields in my excel workbook named:
Country State Cities
All these fields should come as a drop down in UI.
What I want to achieve is: 'when I make a selection for Country, a new drop down for State should get created with a list of States that falls under the Country selected. And based on the state selected the underlying cities should get populated in the drop down'
I found a few examples online, but they all were creating a static UI with 3 fixed drop downs in it. How can I implement it dynamically?
Below code helps me achieve it partially, for exm: if I select a country as 'India', it adds another drop down with list of all the states correctly based on the filter used. But once the states gets populated they don't change even if I change the selected value for Country.
Can someone please help me fix it?
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("message", label = "",placeholder = "Type your message here."),
actionButton("send", "Send")
)
)))
Server<- function ( input, output, session)
{
ObsereEvent(input$send, {
insertUI(selector#message", 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 = "#message", 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 = "#message", 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)