eteuler
eteuler

Reputation: 123

output different plots based on user input in shiny

I have a selectInput gadget asking the user to select a y axis. The options are Genre, Experience, and Rating, and I would like to output one of three bar plots i have created (ratebars3, expbars3, and genbars4). Each plot has the same x axis (company) but the graphs vary based on whatever the y is. Here is the code for the select input in my UI:

    selectInput("selecty", label = h3("Select Y axis"),
    choices = list("Rating", "Experience", "Genre")) 

I have been trying some if.. return type statements in the server with no luck. If anyone would know some server code that could display my plots based on what the user inputs, that would be a huge help.

Upvotes: 3

Views: 3560

Answers (1)

eteuler
eteuler

Reputation: 123

I was using the plotly package but it seems that I've answered my own question. I had to create a reactive expression:

Server:

    barplottest <- reactive({
    if ( "Rating" %in% input$selecty) return(bars3)
    if ( "Experience" %in% input$selecty) return(expbars3)
    if( "Genre" %in% input$selecty) return(genbars4) 
    })

   output$barplot <- renderPlotly({   
   dataplots = barplottest()
   print(dataplots)
   }) 

Ui

      selectInput("selecty", label = h3("Select Y axis"),
                   choices = list("Rating", "Experience", "Genre")),
       plotlyOutput("barplot")

Upvotes: 5

Related Questions