Skip to content

Instantly share code, notes, and snippets.

@aagarw30
Last active July 24, 2022 15:53
Show Gist options
  • Save aagarw30/d08c5fb1794cf9b58fa38342db97b697 to your computer and use it in GitHub Desktop.
Save aagarw30/d08c5fb1794cf9b58fa38342db97b697 to your computer and use it in GitHub Desktop.
Demo updateselectInput() and also introducing observeEvent() function
Title: Use case - Demo updateselectInput() and also introducing observeEvent() function
Description: Powered by R, Shiny, and RStudio.
License: GPL-3
Author: Abhinav Agrawal
DisplayMode: Showcase
Tags: R, R Shiny,updateselectInput(), observeEvent()
Type: Shiny
# global.r
# Defining a dummy dataframe for the sake of demo
# The dataset / objects/ variables from global.r is accessible to both the server.r and ui.r files
# placed in the working directory
# creating dataframe object with 3 variables namely, Year, Month & Person name
# this dataframe might not make sense but just for example
data = data.frame(Year=c("2002","2003","2004","2003","2001","2002", "2001"),
Month = c("Jan", "Feb", "Mar", "Jan", "Dec", "Jan", "Nov"),
Name =c("Sam", "Paul", "James", "Ana","Rose", "Juan", "Tim"),
row.names=NULL, stringsAsFactors = FALSE)
library(shiny)
shinyServer(function(session, input, output){
# observerEvent() function can be used in cases when an action needs to be performed based on event
# In this case we want the second selectInput() choices to be updated based on the changes in
# the first select input()
# Update based on the year change event
# bascially populate the months corresponding to the year
observeEvent(
input$Year,
updateSelectInput(session, "Month", "Month",
choices = data$Month[data$Year==input$Year]))
# Update as soon as Month gets populated according to the year and month selected
observeEvent(
input$Month,
updateSelectInput(session, "Name", "Name",
choices = data$Name[data$Month==input$Month & data$Year==input$Year]))
# just to display the dataset we created in global.r
output$dataset <- renderTable({
data
})
})
library(shiny)
shinyUI(fluidPage(
titlePanel("Demo updateselectInput() and also introducing observeEvent() function"),
# data is a dummy dataset which is coming from global.r
# using the variables from data dataset we are populating the selectinput
selectInput("Year", "Year", choices = unique(data$Year)),
# Second select input for month - empty - no choice to display.
# This is done intentionally as the idea is to populate this selectinput using
# updateselectinput on the loading of app or change in the year selectinput choice
selectInput("Month", "Month", choices = "", selected = ""),
# Third select input for Person - empty - no choice to display
# This is done intentionally as the idea is to populate this selectinput using
# updateselectinput on the loading of app or change in the year and/or month selectinput choice
selectInput("Name", "Name", choices = "", selected = ""),
tableOutput("dataset")
))
@NabeelahB
Copy link

Hi,
I am trying to do a similar thing. But instead of cascading dropdown lists, i want to update lists dynamically without a hierarchy.

I am having a little tricky situation. Similar to the above one, yet different.

I have a dataframe of 7 variables. Which can be filtered in 6 different ways.

I have 6 input lists(dropdown menu). By default all of them have a null value selected. When even 1 of the list value is selected then the other 5 lists should show values based on the first selection. if 2 lists are chosen then the options in the other lists will be based on these 2 values and so on.
There is no set priority of which input will be selected first. But whichever list is chosen, all the other lists should reflect values accordingly.

so if I take your sample data, I can choose Month first and select "Jan" the available years should show 2002 & 2003. and the names Sam, Ana & Juan.

Help will be appreciated.

Regards,
Nabeelah

@ecomohitpande
Copy link

Hi Nabeelah it is almost impossible to help you without providing a reproducible example and code you using

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment