Создание выпадающих в блестящих с существующими сюжетными графиками - PullRequest
1 голос
/ 22 апреля 2020

Я очень плохо знаком с 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», et c.

1 Ответ

0 голосов
/ 22 апреля 2020

Попробуйте это:

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)
...