简体   繁体   中英

R: filter result from selectInput by renderUI shiny with if condition

In the example below, I have four selectInput, but in my real shiny app I actually have six. I would like the selection in the first tree selectInput(major,gender,course) to change the possible selections in the last one(studentname). So the first three selectInput works like filters of the last one.
  • For example, if the user chooses "male" in gender selectInput, then the options in studentname selectInput should only displays the name of male student.
  • If user chooses "male" in gender selectInput and "A" in major selectInput, then the options in studentname selectInput should only display the name of male student in major A.

I have some code below, but it doesn't work well. Thanks a lot if anyone could help!

Here is data example
 df = data.frame(Studentname = c("aa","aa","aa","bb","bb","bb","cc","cc","dd","ee","ff","gg"), Major = c("A","A","B","B","B","B","C","C","A","A","C","C"), Gender = c("female","female","female","male","male","male","male","male","female","female","male","male"), Course = c("01","02","03","01","03","04","02","04","01","03","02","04"),stringsAsFactors=F) 
code:
  library(shiny) ui = (fluidPage( titlePanel("Test"), sidebarLayout( sidebarPanel( uiOutput("choose_maj"), uiOutput("choose_gen"), uiOutput("choose_cou"), uiOutput("choose_stu") ), mainPanel() ) )) server = function(input,output,session){ output$choose_maj = renderUI({ selectInput("maj.in","Choose Major", choices = c("All",unique(df$Major)),selected="All") }) output$choose_gen = renderUI({ selectInput("gen.in","Choose Gender", choices= c("Both",unique(df$Gender)),selected = "Both") }) output$choose_cou = renderUI({ selectInput("cou.in","Choose Course", choices= c("All",unique(df$Course)),selected = "All") }) output$choose_stu = renderUI({ if(input$maj.in != "All"){ dat <- df[which(df$Major == input$maj.in),] } if(input$gen.in != "Both"){ dat <- df[which(df$Gender == input$gen.in),] } if(input$cou.in != "All"){ dat <- df[which(df$Course == input$cou.in),] } selectInput("stu.in", "Choose Student Name", choices = as.list(unique(dat$Studentname)), selected = "All") }) } runApp(list(ui = ui, server = server)) 

If you just provide a copy of your data.frame to your renderUI function you can do all the different subsets.

This works for me:

output$choose_stu = renderUI({

    dat <- df

    if(input$maj.in != "All"){
        dat <- dat[which(dat$Major == input$maj.in),]

    }
    if(input$gen.in != "Both"){
        dat <- dat[which(dat$Gender == input$gen.in),]

    }
    if(input$cou.in != "All"){
        dat <- dat[which(dat$Course == input$cou.in),]
    }

    selectInput("stu.in", "Choose Student Name", 
                choices  = as.list(unique(dat$Studentname)),
                selected = "All")
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM