Emery
Emery

Reputation: 31

Reactive Shiny Plot doesn't display any plot.

I have a dataset with home values for all 50 states over ~30 years. Columns include State, Year, Value, etc. I am trying to create an interactive Shiny app where the user can select certain states so they will be the only ones displayed in the plot. I have successfully created the plot of all states independently where Year is on the x-axis and Value is on the y-axis, colored by State, and also have successfully subset the dataset so that only one state plots.

I am new to Shiny and having issues having anything other than the Input checkBox feature work in this. Is there something obvious I am missing?

    ui <- fluidPage(
       checkboxGroupInput(inputId = "state", label = "States", choices = levels(AllData2$STATE),
        plotOutput(outputId = "hist")))

    server <- function(input, output) {
         output$hist <- renderPlot({
      plot(data = subset(AllData2, AllData2 == input$state), mapping = aes(x = Date, y = HomeValue, 
     color = STATE) + geom_line(size=2, alpha=0.8) + 
     scale_y_continuous(breaks = seq(0, 1000000, 50000)) + 
     scale_x_continuous(breaks = seq(1975, 2020, 5)) +
     ylab("Value in Dollars") + xlab("Year"))

     })
   }

    shinyApp(ui = ui, server = server)

I get no output in my Shiny App except the checkbox options. Thank you for any help.

Upvotes: 0

Views: 2325

Answers (1)

Vedha Viyash
Vedha Viyash

Reputation: 728

There are only syntax errors in your code. Many of them:

  1. You have included plotOutput() inside the checkbox group, please place it outside it.
  2. Use ggplot() instead of plot()
  3. You have included everything inside plot() If you use ggplot() the syntax is: ggplot(data=AllData,mapping=aes())+geom_line()+scale_y_continuous()+scale_x_continuous()+labs(x="This is X label",y="This is ylab",color="This is the color legend label")

Your code will work after fixing these problems Just copy paste this if you want instant result:

library(shiny)
library(ggplot2)
library(dplyr)
ui <- fluidPage(
  column(12,checkboxGroupInput(inputId = "state", label = "States", choices = c(1,2,3,4,5))),
                     column(12,plotOutput(outputId = "hist")))

server <- function(input, output) {
  output$hist <- renderPlot({
    ggplot(data = subset(AllData2, AllData2$STATE %in% input$state), mapping = aes(x = Date, y = HomeValue, 
              color = STATE)) + geom_line(size=2, alpha=0.8) + 
           scale_y_continuous(breaks = seq(0, 1000000, 50000)) + 
           scale_x_continuous(breaks = seq(1975, 2020, 5)) +labs(x="Value in Dollars",y="Year")

  })
}

shinyApp(ui = ui, server = server)

Upvotes: 1

Related Questions