在shiny中用现有的plotly情节创建下拉菜单。

问题描述 投票:0回答:1

我是一个非常新的R,所以这可能是超级明显的,但我真的卡住了!我有五个现有的plotly图表已经创建了。

我已经创建了五个现有的plotly图表。我希望能够从一个下拉菜单中选择它们。我不能让现有的图表名称和下拉菜单之间的链接工作。

我最近的尝试(不成功)。

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 "时显示出来等等

r shiny plotly
1个回答
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)
© www.soinside.com 2019 - 2024. All rights reserved.