2016-05-02 151 views
1

我試圖創建一個閃亮的應用程序,允許我過濾聯賽數據。這是該數據集的例子:鏈接閃亮的選擇參數

list <- c(2.3, 2.5, 2.6, 2.8, 4.12, 5.3, 6.2, 8.2) 
team <- c("A", "A", "A", "A", "B", "B", "B", "B") 
time <- c(1,2,3,4,5,6,7,8) 
league <- c("A", "B","A", "B","A", "B","A", "B") 

df <- data.frame(list, team, time, league) 

林現在使用下面的腳本繪製數據:

#ui script 
shinyUI(fluidPage(
titlePanel("Basic widgets"), 

fluidRow(

    column(3, 
     h3("Buttons"), 

     selectInput("select", label = h3("Select box"), 
        choices = list("Choice 1" = 1, "Choice 2" = 2), selected = 1)), 

    mainPanel(
    plotOutput("distPlot") 
    ) 
) 
)) 

而且服務器腳本:

library(shiny) 

shinyServer(function(input, output) { 

output$distPlot <- renderPlot({ 

list <- c(2.3, 2.5, 2.6, 2.8, 4.12, 5.3, 6.2, 8.2) 
team <- c("A", "A", "A", "A", "B", "B", "B", "B") 
time <- c(1,2,3,4,5,6,7,8) 
league <- c("A", "B","A", "B","A", "B","A", "B") 

df <- data.frame(list, team, time, league) 
ggplot(df, aes(x = time, y = list, colour = team)) + geom_line() 
}) 
}) 

這表明我的線兩個聯賽。但是我想實現的是我可以選擇「Choice1」和「Choice2」,並在圖中顯示相關的數據點。任何建議如何將選擇選項與獨特的聯盟價值聯繫起來?

回答

0

這並不完全清楚你的預期輸出是什麼。但是,我想你問的是如何讓情節改變給予selectInput的選擇?

如果是這種情況,您需要通過過濾給定選項的數據,使得您的圖被動。在代碼中查看我的意見以獲取更多詳細信息。

注:我做了你的應用程序到single-file shiny app

library(shiny) 
library(ggplot2) 

ui <- fluidPage(
    titlePanel("Basic widgets"), 

    fluidRow(

     column(3, 
        h3("Buttons"), 

        selectInput("select", label = h3("Select box"), 
              choices = list("A", "B"), selected = 1, multiple = TRUE)), 

     mainPanel(
      plotOutput("distPlot") 
     ) 
    ) 
) 

server <- function(input, output) { 

    list <- c(2.3, 2.5, 2.6, 2.8, 4.12, 5.3, 6.2, 8.2) 
    team <- c("A", "A", "A", "A", "B", "B", "B", "B") 
    time <- c(1,2,3,4,5,6,7,8) 
    league <- c("A", "B","A", "B","A", "B","A", "B") 

    df <- data.frame(list, team, time, league) 

    output$distPlot <- renderPlot({ 

     ## make the plot reactive depending on the input of the selectInput 'select'. 
     df_plot <- df[df$league %in% input$select, ] 
     ## every time you make a change to selectInput 'select', this plot will update 

     ggplot(df_plot, aes(x = time, y = list, colour = team)) + 
      geom_line() 

    }) 
} 

shinyApp(ui = ui, server = server)