使用现有的绘图创建闪亮的下拉菜单

时间:2020-04-22 13:53:10

标签: r shiny plotly

我是R的新手,所以这可能非常明显,但是我真的很困惑!

我已经创建了五个现有的绘图图表。我希望能够从下拉列表中选择闪亮的它们。我无法在现有图表名称和下拉列表之间建立链接。

我最近的尝试(无效):

ui <-shinyUI(fluidPage(selectInput("selectPlot", "Select Year:", 
choices = c("2015", "2016", "2017", "2018", "Average price across US"), 
selected = "Average price across US", plotlyOutput("plot"))))

server <- shinyServer(function(input,output){    
  output$plot <- renderPlotly({
    if(input$selectPlot == '2015') {
      p <- gg1
    }
    if(input$selectPlot == '2016') {
      p <- gg2
    }
    if(input$selectPlot == '2017') {
      p <- gg3
    }
    if(input$selectPlot == '2018') {
      p <- gg4
    }
    if(input$selectPlot == 'Average price across US') {
      p <- gg5
    }
    return(p)
  })
})

shinyApp(ui,server)

我正在尝试让gg1在选择“ 2015”时显示,等等。

1 个答案:

答案 0 :(得分:0)

尝试一下:

library(shiny)
library(plotly)

ui <- shinyUI(
    fluidPage(
        selectInput("selectPlot", "Select Year:", c("2015", "2016", "2017", "2018", "Average price across US"), 
                    selected = "Average price across US"),
        plotlyOutput("plot")
    )
)

server <- shinyServer(function(input,output,session){   

    data <- eventReactive(input$selectPlot,{
        switch(input$selectPlot,
               "2015" = gg1,
               "2016" = gg2,
               "2017" = gg3,
               "2018" = gg4,
               "Average price across US" = gg5)
    })

    output$plot <- renderPlotly({
        data()
    })
})

shinyApp(ui,server)