How to make the second sidebar drop down based on the selection of the first side bar in Rshiny

I want to create a shiny app that contains two side bars and the second side bar's drop down is based on the result of the first side bar. I have to datasets from MySQL database and store in data frame, I want to make a Rshiny app which user can first choose which datasets they want to visualize at the first side bar, and then choose which Participants to visualize in the second side bar. The dataset contains the Participants, their races number, and scores.

Below is the datasets:

Participant| Race_number|Score
P1|R1|2
P1|R2|3
P2|R1|4
P2|R2|7
P3|R1|8
P3|R2|9
P4|R1|9
P4|R2|5

Dataset2
Participant| Race_number|Score
P1|RR1|2
P1|RR2|7
P2|RR1|4
P2|RR2|7
P3|RR1|9
P3|RR2|9

The first side bar with a drop down showing "Dataset1" and "Dataset2", after the users choose Dataset1 for example, the second side bar will show a drop down with "P1-P4", If the users choose Dataset2, the second side bar will show a drop down with "P1-P3".

Below is my code so far and when I run the app the second slide bar's drop down doesn't show up.

library(shiny)

library(DBI)
library(RMySQL)



# Define UI for application that draws a histogram
ui <- fluidPage(
    titlePanel("Participants"),
    sidebarLayout(
        sidebarPanel(
            selectInput("Dataset","Please select a dataset:",choices = c("Dataset1", "Dataset2")),
            uiOutput("Participants")
            ),

        # Show a plot of the generated distribution
    mainPanel(
           plotOutput("barplot")
           )
    )
)
        
    


# Define server logic required to draw a histogram
server <- function(output, input)({
    var <- reactive({
        switch(input$Dataset,
               "Dataset1" = names(Dataset1$Participant),
               "Dataset2" = names(Dataset2$Participant)
    })
    
        


    output$Participants <- renderUI({
selectInput("Participants", "Select the participant", choices = var())
    })

})
    
    
    

    
# Run the application 
shinyApp(ui = ui, server = server)

Can anyone tell me why my code doesn't work?

In my experience, those are due to extra commas in your UI code. I see you have an extra comma after your uiOutput call. Try removing that to close out the sidebarPanel

Thank you for pointing out! I removed it and also updated the code here. But now I have another error saying:Error in func(fname, ...) : app.R did not return a shiny.appobj object.

Sorry I’m on my phone and can’t run. I also notice you aren’t defining ui and server when calling shinyApp. For your ui, remove “ShinyUI” function and replace with ui assignment, so “ui = “. Same for server - remove “shinyServer“ and replace with “server = “

I noticed it and fixed. Now I can run the app but the second side bar's drop down not showing up as expected.

Are you taking the names of a dataframe column? That would give you a NULL, right? Are you trying to select from a unique set of participants from each dataframe? If so, do unique(Dataset1$Participant)..

Yes! It works! Thank you!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.