2016-06-28 56 views
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它應該顯示德國和西班牙。

簡而言之,我想創建地區和國家的依賴下拉菜單。國家下拉菜單應顯示基於該地區的國家。

它應該能夠產生的情節,以及基於該評級列

我有我的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)) 
}) 
}) 

回答

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) 

由於我不知道您的最終申請,劇情的標題取決於評級列。

+0

謝謝。我想要的是,它應該根據BarPlot中的選擇顯示平均評分。我使用barplot函數使用book3 $ rating,但不會繪圖。 –

+0

的平均評分?你每個國家只有一個觀察點? –

+0

這對我有用:'barplot(book3 [which(book3 $ country == input $ data2),] $ Rating,ylim = c(0,10))''。 –