callin
callin

Reputation: 49

Why does my plot disappear in shiny when I add a table?

I have created a shiny app should take input from three sliders and:

  1. Plots a distribution in ggplot
  2. Show a summary table of values underneath the plot in #1 above

If I just want to plot the histogram (and I comment out the table data), I can get the code to work correctly. However, when I add the table, the plot disappears even though the plot header is still there. I have tried moving the commas a braces around to see if it's a simple syntax error but haven't had any luck.

library(shiny)

# Define UI for application that draws a histogram
shinyUI(fluidPage(

    # Application title
    titlePanel("Test Shiny Layout"),

    # Sidebar with a slider input for number of bins
    sidebarLayout(
        sidebarPanel(
            h4("Input Data"),
            sliderInput("bins", "Bin Width", min = 4,max = 12, value = 8),
        ),
        # Show a plot of the generated distribution
        mainPanel(
            h4("Histogram"),
            plotOutput("distPlot", width = "600", height = "600"),
            h4("Table of Values"),
            tableOutput("table")
        )
    )

))

Server

library(shiny)
library(ggplot2)

# Define server logic required to draw a histogram
shinyServer(function(input, output) {
        output$distPlot <- renderPlot({
        bins <- input$bins 
        df1 <- (iris$Sepal.Length)
        x <- mean(df1)
        y <- sd(df1)


        ggplot(data = iris) +
            geom_histogram(mapping = aes(x = Sepal.Length), color = "blue", binwidth = "bins")

# Create an empty dataframe and then plug in the mean and standard deviation
        results <- data.frame("0", "0")
        results[1,1] = x
        results[1,2] = y
        colnames(results) <- c("Mean", "SD")
        rownames(results) <- c("Sepal Length")

        output$table <- renderTable(results)
    })
})

Upvotes: 0

Views: 533

Answers (1)

Limey
Limey

Reputation: 12506

Your renderTable() is inside your renderPlot() call. So renderPlot isn't returning anything.

You were right: it was a simple syntax error. But you also had several other issues in your code. At least a dozen. Three in binwidth = "bins" alone.

Here's a working version. I suspect you will still want to make tweaks, but at least you have both a histogram and a summary table that both look reasonably sensible.

library(shiny)
library(ggplot2)
data(iris)

# Define server logic required to draw a histogram
server <- function(input, output) {
  output$distPlot <- renderPlot({
    ggplot(data = iris) +
      geom_histogram(aes(x = Sepal.Length), color = "blue", bins = input$bins)
  })

  output$table <- renderTable({
    iris %>%
      summarise(Mean=mean(Sepal.Length),
                SD=sd(Sepal.Length))
  })
}

ui <- fluidPage(
  titlePanel("Test Shiny Layout"),
  sidebarLayout(
    sidebarPanel(
      h4("Input Data"),
      sliderInput("bins", "Bin Width", min = 4,max = 12, value = 8),
    ),
    mainPanel(
      h4("Histogram"),
      plotOutput("distPlot", width = "600", height = "600"),
      h4("Table of Values"),
      tableOutput("table")
    )
  )
)

shinyApp(ui = ui, server = server)

Upvotes: 1

Related Questions