0

I have a data frame book3. It has three column Region, Country and Rating.

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)

I want to achieve when i select from drop-down "Americas" Region, it should only display Mexico and when i select Asia Pacific it should display China and India and for EMEA it should display Germany and Spain.

In brief I want to create dependent drop down of Region and Country. The Country drop down should display the country based on the Region.

It should be able to generate the plot as well based on the rating column

I have my code in ui.R and server.R please suggest something

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, session) {
  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))
 })
})

1 Answers1

0

It is very easy and you are almost there:

library(shiny)
test <- "http://www.score11.de"
# Define UI for application that draws a histogram


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


server <- shinyServer(function(input, output, session) {
  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 = book3[which(book3$Region == input$data1),]$Country)
  })

  output$plot <- renderPlot({
    barplot(mean(book3[which(book3$Country == input$data2),]$Rating), ylim=c(0,10), xlim=c(0,2), width=0.5)
  })


})

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

Since I do not know your final application the plot title depends on the Rating column.

Martin Schmelzer
  • 23,283
  • 6
  • 73
  • 98