2016-06-28 6 views
0

私はデータフレームbook3を持っています。それは、3つの列、地域、国と格付けを持っています。従属選択によるプロット

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) 

最終的なアプリケーションがわからないので、プロットのタイトルはRating列に依存します。

+0

ありがとうございます。 BarPlotでの選択に基づいて平均評価が表示されるはずです。私はbarplot関数を使ってbook3 $ ratingを使用しましたが、それはプロットしません。 –

+0

平均評価:あなたは国ごとに1つの観察しかありませんか? –

+0

これは私のために働いています: 'barplot(book3 [book3 $ country == input $ data2]、] $ Rating、ylim = c(0,10))'。 –

関連する問題