How to create dependent drop down

173 views
Skip to first unread message

Rahul Thakur

unread,
Jun 28, 2016, 1:48:34 AM6/28/16
to Shiny - Web Framework for R
Hi ,

I have a data frame book3, it has three columns Region, Country and rating. I want to create a dependent column. see my code below

UI.R

library(shiny)

ui <- fluidPage(
  titlePanel("Test Dashboard "),
  sidebarLayout(
    sidebarPanel(
      uiOutput("data1"),   ## uiOutput - gets the UI from the server
      uiOutput("data2")
    ),
    mainPanel()
  ))


--
Server.r

library(shiny)

shinyServer(function(input, output) {
  Region<- c("Americas", "Asia Pacific","Asia Pacific", "EMEA", "EMEA")
  Country<- c("Mexico", "China","India", "Germany", "Spain" )
  Rating<- c(5,3,3,2,4)
 
  book3<- data.frame(Region, Country, Rating, stringsAsFactors = F)
 
 
  output$data1 <- renderUI({
    selectInput("data1", "Select Region", choices = c(book3$Region))
  })
 
 
  output$data2 <- renderUI({
    selectInput("data2", "select Country", choices = c(book3$Country))
  })
 
 
 
})

Vamsi Krishna

unread,
Jun 28, 2016, 3:04:20 AM6/28/16
to Shiny - Web Framework for R
Hi,

The question isn't clear. You want to create a dropdown depending on one of your columns, is it?

Then do this
in server.R
sel= reactive({
  depBook<- Book3[Book3$Rating>3]
   return (depBook$Region)
  
} )
 observe({
   updateSelectInput(session, inputId = "IDID",
                     choices = sel()
   )})

in ui.R

selectInput('IDID', '"random text', ""),




So here the dropdown would have book3$Regions options  which would be  dependent on book3$Rating 's values

Rahul Thakur

unread,
Jun 28, 2016, 3:13:51 AM6/28/16
to Shiny - Web Framework for R
Hi,

If you run my code it shows up two drop down one for Region and another for Country. I want, when I select Americas it should show Country for Americase (Maxico) and when i select Asia Pacific it should show China, India etc.

and based on selected parameters in ui.R, i want to create a plot.

Regards,
Rahul

Vamsi Krishna

unread,
Jun 28, 2016, 3:40:39 AM6/28/16
to Shiny - Web Framework for R

Hi ,
You 'll have to do something like this '
in server.R


output$data1 <- renderUI({
    selectInput("data1", "Select Region", choices = c(book3$Region))
  })

sel= reactive({
  depBook<- Book3[Book3$Region== input$data1]
   return (depBook$Country)

  
} )
 observe({
   updateSelectInput(session, inputId = "IDID",
                     choices = sel()
   )})


IN ui.R

selectInput('IDID', "select Country ", ""),


 I might be missing some unknown dependencies but this should work fine i believe

Reply all
Reply to author
Forward
0 new messages