绘图基于依赖选择

时间:2016-06-28 07:42:16

标签: r shiny shinydashboard shinyjs

我有一个数据框book3。它有三列Region,Country和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)

当我选择下拉“美洲”地区时,我想实现它应该只显示墨西哥,当我选择亚太时,它应该显示中国和印度,而对于EMEA,它应该显示德国和西班牙。

简而言之,我想创建Region和Country的依赖下拉。国家/地区下拉列表应根据地区显示国家/地区。

它应该能够根据评级栏

生成图表

我的代码在ui.R和server.R请提出建议

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 个答案:

答案 0 :(得分:0)

这很容易,你几乎就在那里:

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)

由于我不知道你的最终申请,情节标题取决于评级栏。