0
votes

I am having some trouble with debugging in the shiny app. It doesn't work after I added a few new things, but it is still able to open up the shiny app platform although some content is blank with an error message. The table and the scatterplot don't work for me. Below is the error that keeps coming up.

Error in output$table <- renderDataTable(trend_data()) : object 'output' not found"

could not find function "y"

I can't figure how to make the table and the plot graph work.



# shape <- st_read("tl_2016_53_cousub.shp")
# sta <- read_csv("NOAA_SeattlePortageBay.csv") %>% #weather station
#   mutate(lon = Longitude, lat = Latitude) %>%
#   st_as_sf(coords = c("LONGITUDE", "LATITUDE"), crs=4326) %>%
#   st_join(shape)

trend_data <- read_csv("NOAA_SeattlePortageBay.csv")

y <- trend_data %>%
  sample_n(0) %>%       
  select("Total Liquid Content", "Extreme Max Precip", "Annual Mean Temp", "Mean Max Temp", "Mean Min Temp") 


ui <- fluidPage(title = "Seattle, Washington 40 Years Climate",
                navlistPanel(
                  tabPanel(title = "Introduction",
                           leafletOutput("map"),
                           textOutput("dis")),

                  tabPanel(title = "Climate Graphs",
                           plotOutput("plot1"),
                           # plotOutput("plot2"),
                           plotOutput("plot3"),
                           plotOutput("plot4"),
                           plotOutput("plot5")),

                  tabPanel(title = "Data Table",
                           tableOutput("table")),

                  tabPanel(title = "Plot Model",
                           plotOutput("scatterplot"),
                           varSelectInput("yvar", "Y Variable:", data=y, selected="Total Liquid Content"))
                )
)

server <- function(input, output) {
  # output$map <- renderLeaflet({   #doesn't work so I use another route
  #   leaflet(data = sta) %>%
  #     addTiles() %>%
  #     addMarkers(~lon, ~lat, label = ~Name)
  #     output$dis <- renderText("Seattle is a city located in the State of Washington.
  #                            Seattle Portage Bay Weather Station by NOAA.")

  output$map <- renderLeaflet({
    leaf <- leaflet() %>%
      addTiles() %>%  # Add default OpenStreetMap map tiles
      addMarkers(lng=-122.3, lat= 47.65,
               popup="Seattle Portage Bay, WA, USA, GHCND:USW00024281")
  })

  output$dis <- renderText("Seattle Portage Bay Weather Station by NOAA.","\r",
                           "Elevatioin: 5.8m", "\r",
                           "Period of Record: January 1, 1894 to January 1, 1997")

  output$plot1 <- renderPlot({                         #Error in output$plot1 <- renderPlot({ : object 'output' not found
    ggplot(trend_data) +                               #unexpected '}' in "}"
      geom_point(aes(Year, trend_data$`Total Liquid Content`),
                 size = 3, color = "dark blue") +
      geom_smooth(aes(Year, trend_data$`Total Liquid Content`), size = 1, color = "black", method = "lm") +
      labs(title = "Total Precipitation",
           x = "Year", y = "Precipitation in Inches") +
      theme(text= element_text(size=15,  family="Arial"), 
            plot.title = element_text(hjust = 0.5)) + ylim(15, 55) +
      scale_x_continuous(breaks = seq(from = 1940, to = 2000, by = 5))
  })

  # output$plot2 <- renderPlot({
  #   ggplot(trend_data) +
  #     geom_point(aes(Year, trend_data$`Extreme Max Precip`),
  #                size = 3, color = "red") +
  #     geom_smooth(aes(Year, trend_data$`Extreme Max Precip`), size = 1, color = "black", method = "lm") +
  #     labs(title = "Extreme Max Precipitation",
  #          x = "Year", y = "Precipitation in Inches") +
  #     theme(text= element_text(size=15,  family="Arial"),
  #           plot.title = element_text(hjust = 0.5)) + ylim(15, 55) +
  #     scale_x_continuous(breaks = seq(from = 1940, to = 2000, by = 5))
  # })

  output$plot3 <- renderPlot({
    ggplot(trend_data) + 
      geom_point(aes(Year, trend_data$`Annual Mean Temp`), 
                 size = 3, color = "brown") + 
      geom_smooth(aes(Year, trend_data$`Annual Mean Temp`), size = 1, color = "black", method = "lm") + 
      labs(title = "Average Temperature",
           x = "Year", y = "Temperature in Fahrenheit") +
      theme(text= element_text(size=15,  family="Arial"), 
            plot.title = element_text(hjust = 0.5)) + ylim(50, 56) +
      scale_x_continuous(breaks = seq(from = 1940, to = 2000, by = 5))
  })

  output$plot4 <- renderPlot({
    ggplot(trend_data) + 
      geom_point(aes(Year, trend_data$`Mean Max Temp`), 
                 size = 3, color = "red") + 
      geom_smooth(aes(Year, trend_data$`Mean Max Temp`), size = 1, color = "black", method = "lm") +
      labs(title = "Average Maximum Temperature",
           x = "Year", y = "Temperature in Fahrenheit") +
      theme(text= element_text(size=15,  family="Arial"), 
            plot.title = element_text(hjust = 0.5)) + ylim(57, 64) +
      scale_x_continuous(breaks = seq(from = 1940, to = 2000, by = 5))
  })

  output$plot5 <- renderPlot({
    ggplot(trend_data) + 
      geom_point(aes(Year, trend_data$`Mean Min Temp`), 
                 size = 3, color = "light blue") + 
      geom_smooth(aes(Year, trend_data$`Mean Min Temp`), size = 1, color = "black", method = "lm") + 
      labs(title = "Average Minimum Temperature",
           x = "Year", y = "Temperature in Fahrenheit") +
      theme(text= element_text(size=15,  family="Arial"), 
            plot.title = element_text(hjust = 0.5)) + ylim(43, 50) +
      scale_x_continuous(breaks = seq(from = 1940, to = 2000, by = 5))
  })

  output$table <- renderDataTable(trend_data())

  output$scatterplot <- renderPlot({
    ggplot(data = y()) +              #can't find function y
      geom_point(mapping = aes(x = Year, y = !!input$yvar)) +
      geom_smooth(mapping = aes(x = Year, y = !!input$yvar), method="lm")
  })

}

shinyApp(ui = ui, server = server)
1

1 Answers

0
votes
output$table <- renderDataTable(trend_data())

In the above code your trend_data is not a reactive dataset. You can either

  1. Make trend_data a reactive dataset which will change based on some reactivity in your Shiny App.

    trend_data <- reactive({ #some code here })

  2. Or use the trend_data as it is

    output$table <- renderDataTable(trend_data)

And in plot1 error remove trend_data$

output$plot1 <- renderPlot({                        
    ggplot(trend_data) +                       
      geom_point(aes(Year, `Total Liquid Content`),
                 size = 3, color = "dark blue") +
      geom_smooth(aes(Year, `Total Liquid Content`), size = 1, color = "black", method = "lm") +
      labs(title = "Total Precipitation",
           x = "Year", y = "Precipitation in Inches") +
      theme(text= element_text(size=15,  family="Arial"), 
            plot.title = element_text(hjust = 0.5)) + ylim(15, 55) +
      scale_x_continuous(breaks = seq(from = 1940, to = 2000, by = 5))
  })