0

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.

4
  • 1
    can you provide a minimal example also with sample data? So we can replicate your error. I'm not familiar with basic plot function, but it seems like you are using ggplot syntax in basic plot function? Commented May 18, 2018 at 23:07
  • Also, are you sure you're subsetting the data properly? Perhaps it should be something like subset(AllData2, AllData2$STATE %in% input$state) because the checkboxGroupInput returns a character vector of selected values. Commented May 18, 2018 at 23:13
  • @YifuYan yes I have tried ggplot, too; it was my original syntax. I will try and provide a sample dataframe by tomorrow. Commented May 18, 2018 at 23:26
  • @Blaza, thank you. One of my main questions was "what does checkboxGroupInput return" and how does that affect the reactiveness of what I am trying to get at. Regardless, I can't even get a plot to show up, even if it is a non-reactive simple ggplot of my initial tidy data. Commented May 18, 2018 at 23:28

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Wow, thank you so much. I just changed the choices = c() back to the original levels(AllData2$State) and it works perfectly. Thank you!
I'm glad I could help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.