Display Plot in Shiny

Hi,
Having an issue with the renderplot function in Shiny. For some reason, Shiny does not display the graph "scatter" in the UI and I don't understand why. Everything else is displayed correctly. Please find below my code. Any reason why the plot is not displayed in the main panel? Please find the code here:

Further, but less important, I find my filtering quite repetitive as I filter for each output object. It is running well with good performance, but still, maybe there is a leaner way :slight_smile:

Thank you for your support!

You need to call the plot in order to know what should be shown. So add a line with "scatter" or "return(scatter)" as last line within the "output$scatter <- renderPlot({ })"

Here a simple example:

library(shiny)
library(dplyr)
library(magrittr)

# Define UI for application that draws a histogram
ui <- fluidPage(

  # Application title
  titlePanel("Old Faithful Geyser Data"),

  # Sidebar with a slider input for number of bins 
  sidebarLayout(
      sidebarPanel(
          sliderInput("bins",
                      "Number of bins:",
                      min = 1,
                      max = 50,
                      value = 30)),

      # Show a plot of the generated distribution
      mainPanel(
         plotOutput("distPlot")
      )
  )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

output$distPlot <- renderPlot({

     faithful_data = faithful %>% 
         mutate(waiting_mins = waiting/60)
     
     # draw the histogram with the specified number of bins
     plot = ggplot(faithful_data, aes(x = waiting_mins)) +
         geom_histogram(bins = input$bins)
     
     # you need to call the plot so it is rendered and shown!!!
     plot
     # or : 
     # return(plot)
            
     })
}

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

Haha, stupid me. Thanks so much, Matthias. Thought you don't need to call it as in other functions like textOutput. It works as desired now.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.