0

I have trying to make an app that requires zip code input. I want to filter the data based on the targeted zip code the user provides and plot the time series forecast for housing data. I am at a loss and keep getting an error message in the plot box when I run the app. This is my first time working with Shiny and don't know the first step to getting the filtered data.

Also my "help" button gets me an error too if anyone can help with that. These features might be too unique for help from the internet.

library(shiny)
install.packages("shinydashboard")
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "Market Prediction"),
  dashboardSidebar(
    actionButton("pdf", "Help"),
    downloadButton("downloadData", "Market Analysis")),
  dashboardBody(
    box(plotOutput("market_analysis"), width = 8),
    box(
      selectInput("Zip_Code", "Enter Zip Code",
                  c(unique(Home_Clean1$RegionName))))
    )
  )

server <- function(input,output, session){
filteredData <- reactive({
  filterPattern <- input$Zip_Code
  filtered <- Home_Clean1[grep1(filterPattern, Home_Clean1$RegionName), ]
  return(filtered)
})
  output$market_analysis <- renderPlot({
    plot(filtered ,aes(x = Monthly_parsed, y=Price/1000)) +
      geom_line(color = "blue") +
      xlab("Year") +
      ylab ("Price in Thousands")+
      ggtitle("House Prices Over Time") +
      scale_x_date(date_labels = "%Y", breaks = "10 year") +
      theme(axis.text.x = element_text(angle = 90,hjust = 1))

  })
  observeEvent(input$pdf, {
    file.show("C:/Users/gorge/OneDrive/Document/OneDrive/Documents/DSC Capstone/Help.pdf"(R.home(), "doc", "Help.pdf"))
 })


}

shinyApp(ui, server)`

enter image description here

Phil
  • 7,287
  • 3
  • 36
  • 66

1 Answers1

0

That is because filtered doesn't exist in the context you're using it in. You need to call your reactive e.g. filteredData() in the plot function.

Untested, since we don't have reproducible data. Next time it would be helpful to include some of your data or sample data.

filteredData <- reactive({
  filterPattern <- input$Zip_Code
  filtered <- Home_Clean1[grep1(filterPattern, Home_Clean1$RegionName), ]
  return(filtered)
})

# Use filteredData() not filtered below

  output$market_analysis <- renderPlot({
    plot(filteredData() ,aes(x = Monthly_parsed, y=Price/1000)) +
      geom_line(color = "blue") +
      xlab("Year") +
      ylab ("Price in Thousands")+
      ggtitle("House Prices Over Time") +
      scale_x_date(date_labels = "%Y", breaks = "10 year") +
      theme(axis.text.x = element_text(angle = 90,hjust = 1))

  })

In general you should keep each SO post limited to a single question especially if they're unrelated. As an FYI, file.show is only for plain text files and you are trying to include a pdf so that will not work.

I would check out this question for some help on it. displaying a pdf from a local drive in shiny

Hope that helps!

Jamie
  • 1,793
  • 6
  • 16