0

我有一个数据框 book3。它具有三列区域、国家和评级。

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,它应该显示德国和西班牙。

简而言之,我想创建地区和国家的相关下拉列表。Country 下拉菜单应显示基于 Region 的国家/地区。

它也应该能够根据评级列生成图

我在 ui.R 和 server.R 中有我的代码,请提出一些建议

用户界面

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

服务器.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))
 })
})
4

1 回答 1

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)

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

于 2016-06-28T08:20:31.840 回答