I am trying to use selectInput to select through different ggplots entirely using renderPlotly

Hi, I am extremely new to R and R Shiny (as in a few days.) and I have come across a problem where my R Shiny app is not changing between ggplots when I select the different options in the selectInput drop Down.

library(shiny)
library(plotly)
ui <- fluidPage(

    # title
    titlePanel("Metric Tracker"),
 
    sidebarLayout(
        sidebarPanel(
            selectInput("value", "Select Value" , choices = c("Ordered_Product_Sales", "Units_Ordered"), selected = NULL ,  multiple = FALSE, selectize = TRUE)
          
        ),

        mainPanel(
           plotlyOutput("valuePlot"),
           
        )
    )
)

server <- function(input, output) { 
    
name <- reactive({
    (input$value)
})    
    
output$valuePlot <- renderPlotly({
    if (input$value == name())
        metric2 %>%
            ggplot(aes(x=Date, y = Ordered_Product_Sales, title = "Sales")) + geom_point() + geom_line( ) + geom_smooth(method = 'lm') + facet_wrap( ~ Brand)

    if (input$value == name())
        metric2 %>% 
            ggplot(aes(x=Date, y = Units_Ordered)) + geom_point() + geom_line() + geom_smooth(method = 'lm') + facet_wrap( ~ Brand)
      

        
 #           ggplot(aes(x=metric2$Date, y = metric2[,9])) + geom_point() + geom_line() + geom_smooth(method = 'lm') + facet_wrap( ~ Brand)
        
    })

}
shinyApp(ui = ui, server = server)

Here metric2 is my data set and Ordered_Product_Sales and Units_Ordered are the variables i am trying to plot against the Date. Currently only one of the graphs appears when I run the app, and it doesn't change when another option is chosen.

I am sure I have made a very obvious mistake or there is a way more efficient way, so thanks for your patience.

Hey, just FYI, there's a stray comma following your call to plotlyOutput() that causes an error when I copy and paste your code.

Also, it would be very helpful to have a full reproducible example, or reprex, so people can see the exact problem you're having and debug it. It looks like you're pretty close to one, which is great! I think it would all run if you created some dummy data for metric2. See the reprex FAQ:

And here's the FAQ on debugging a shiny app:

Just glancing through your code, I do note a couple things:

  • renderPlotly() takes an expression (or maybe function? I'm not certain) as the first argument (The stuff surrounded by {}), and that expression only returns the last line evaluated, so for displaying 2 plots, you'll need 2 calls to renderPlotly(). Is your app currently displaying only the second plot?
  • Your if() conditions will never evaluate to FALSE.
  • Your plots don't access any input values, so they will never update.