Select which plot to display in a shiny app based on user choice

I have a shiny app which generates 2 plots and one table. As you will see I want to select which one of them will be displayed every time based on the radiobuttons() input. Until now I get an error object of type 'closure' is not subsettable Which object exactly is of type closure? Note that the first is a ggplot object the second is a plotly object and the third a datatable.

    # ui.R
        library(shiny)
        library(plotly)
        pageWithSidebar(
          headerPanel('Iris k-means clustering'),
          sidebarPanel(
            uiOutput("filter_degree")

          ),
          mainPanel(
            uiOutput('plot')

          )
        )
        #server.r
        function(input, output, session) {
          output$filter_degree<-renderUI({
            radioButtons("rd","Select Option",choices = c("Mileage","Regression",'Table'),
                         selected = "Mileage")
          })


          output$plot <- renderUI({
           if(input$rd=="Mileage"){
             output$plot1<-renderUI({
            # Boxplots of mpg by number of gears 
            # observations (points) are overlayed and jittered
            qplot(gear, mpg, data=mtcars, geom=c("boxplot", "jitter"), 
                  fill=gear, main="Mileage by Gear Number",
                  xlab="", ylab="Miles per Gallon")
             })
            }


            else if(input$rd=="Regression"){
              output$plot2<-renderUI({
            x <- c(1:100)
            random_y <- rnorm(100, mean = 0)
            data <- data.frame(x, random_y)

            p <- plot_ly(data, x = ~x, y = ~random_y, type = 'scatter', mode = 'lines')
              })
            }


            else if(input$rd=="Table"){

              output$tbl =  DT::renderDataTable(datatable(
                iris, options = list(lengthChange = FALSE,scrollY = T, scroller = TRUE, scrollX = T),selection = list(target="cell",mode="single"),rownames = F)
              )
            }

          })

        }

1 answer

  • answered 2018-12-05 20:46 Ryan Morton

    You need to provide the plot/table output as part of the if/then sequence (plotOutput("plot1"), etc.). Otherwise, it has nothing to render. Also, there appears to be an error in the ploty call, but I haven't fixed it for you.

    library(shiny)
    library(DT)
    library(plotly)
    ui <- pageWithSidebar(
      headerPanel('Iris k-means clustering'),
      sidebarPanel(
        uiOutput("filter_degree")
    
      ),
      mainPanel(
        uiOutput('plot')
    
      )
    )
    #server.r
    server <- function(input, output, session) {
      output$filter_degree<-renderUI({
        radioButtons("rd","Select Option",choices = c("Mileage","Regression",'Table'),
                     selected = "Mileage")
      })
    
    
      output$plot <- renderUI({
        if(input$rd=="Mileage"){
          output$plot1<-renderPlot({
            # Boxplots of mpg by number of gears 
            # observations (points) are overlayed and jittered
            qplot(gear, mpg, data=mtcars, geom=c("boxplot", "jitter"), 
                  fill=gear, main="Mileage by Gear Number",
                  xlab="", ylab="Miles per Gallon")
          })
          plotOutput("plot1")
        }
    
    
        else if(input$rd=="Regression"){
          output$plot2<-renderUI({
            x <- c(1:100)
            random_y <- rnorm(100, mean = 0)
            data <- data.frame(x, random_y)
    
            p <- plot_ly(data, x = ~x, y = ~random_y, type = 'scatter', mode = 'lines')
          })
          plotlyOutput("plot2")
        }
    
    
        else if(input$rd=="Table"){
    
          output$tbl =  DT::renderDataTable(datatable(
            iris, options = list(lengthChange = FALSE,scrollY = T, scroller = TRUE, scrollX = T),selection = list(target="cell",mode="single"),rownames = F)
          )
    
          dataTableOutput("tbl")
        }
    
      })
    
    }
    shinyApp(ui = ui, server = server)