1

I am creating a shiny app in which the user can select the plot to view. I used this question as an approach, but I am getting an error. How can I fix this?

UI

library(shiny)
library(shinydashboard)
library(shinythemes) 
library(tidyverse)

ui =   navbarPage("Project ", theme = shinytheme("cyborg")
                  uiOutput("all"),
       tabPanel("Plot",
                      icon = icon("chart-area"),
                      sidebarLayout(sidebarPanel(
                                    selectInput("Plot", "Please select a plot to view:",
                                                 choices = c("Plot-1", "Plot-2")),
                                    submitButton("Submit")),
                      plotOutput(outputId = "Plots",
                                   width = "1024px",
                               height = "768px")
              )))

Server

server = function(input, output, session) {

data = eventReactive(input$Plot,{
            switch(input$Plot,
                   "Plot-1" = Plot1,
                   "Plot-1" = Plot2)
        })

output$Plots = renderPlot({

             data("midwest", package = "ggplot2") # Sample data

              Plot1 = ggplot(midwest, aes(x=area, y=poptotal)) +
                      geom_point()
              Plot2 =  ggplot(midwest, aes(x=area, y=poptotal)) + geom_point() + 
                       geom_smooth(method="lm")
 
        })
        

}

# Run the application 
shinyApp(ui = ui, server = server)

Error

enter image description here

Ed_Gravy
  • 1,841
  • 2
  • 11
  • 34

1 Answers1

1

Here's a way -

library(shiny)
library(shinydashboard)
library(shinythemes)

ui =   navbarPage("Project ", theme = shinytheme("cyborg"),
                  uiOutput("all"),
                  tabPanel("Plot",
                           icon = icon("chart-area"),
                           sidebarLayout(sidebarPanel(
                             selectInput("Plot", "Please select a plot to view:",
                                         choices = c("Plot-1", "Plot-2")),
                             actionButton("submit", "Submit")),
                             plotOutput(outputId = "Plots",
                                        width = "1024px",
                                        height = "768px")
                           )))

server = function(input, output, session) {
  
  observeEvent(input$submit,{

      Plot1 = ggplot(midwest, aes(x=area, y=poptotal)) +
        geom_point()
      Plot2 =  ggplot(midwest, aes(x=area, y=poptotal)) + geom_point() + 
        geom_smooth(method="lm")

  output$Plots = renderPlot({
    switch(isolate(input$Plot),
           "Plot-1" = Plot1,
           "Plot-2" = Plot2)
  })
  
  })
  

  
  
}

# Run the application 
shinyApp(ui = ui, server = server)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • Wouldn't it be more efficient to call `ggplot` inside the switch statements? This way, the graphs are both constructed when only one of them is needed to be returned. – Jan Oct 02 '21 at 09:02
  • Yes, I think you are right that would be more efficient. – Ronak Shah Oct 02 '21 at 10:22