I am trying to make an r Shiny app in which selecting a radio button will give you a different graph depending on which drop down you select, using USGS streamflow data from the dataRetrieval() package.
When I run my app, the first radio button, which selects for "mean" works perfectly. When selecting the second radio button, which should give a graph of maximum values, I get an error saying that my y variable cannot be found: Error: object 'X_00060_00002' not found
When checking to see if that is even the correct variable name, I get the error object "maximum" not found. So is the function not even reading my second readNWISdv()?
I have tried looking at other posts but the solutions all seem so confusing! I am pretty new to r in general.
require(shiny)
require(dataRetrieval)
require(lubridate)
require(raster)
require(ggplot2)
ui = fluidPage(
selectInput(inputId = "usgs_site", label = "select usgs site", choices = c("Colorado below Glenwood" = "09085100", "Colorado near Cameo" = "09095500",
"Colorado near Utah" = "09163500")),
radioButtons(inputId = "table", label = "Choose Mean or Max", choices = c("Mean" = "1", "Max" = "2")),
plotOutput(outputId = "timeseries")
)
server = function(input, output){
output$timeseries = renderPlot({
river = readNWISdv(siteNumbers = input$usgs_site, parameterCd = "00060", statCd = "00003",
startDate = "", endDate = "")
river$Date = ymd(river$Date)
maximum = readNWISdv(siteNumbers = input$usgs_site, parameterCd = "00060", statCd = "00002",
startDate = "", endDate = "")
maximum$Date = ymd(maximum$Date)
if(input$table == 1) {plot(X_00060_00003 ~ Date, data = river, type = "l", ylab = "Discharge, cfs", xlab = "Year", col = "blue")}
if(input$table == 2) {plot(X_00060_00002 ~ Date, data = maximum, type = "l", ylab = "Discharge, cfs", xlab = "Year", col = "blue") }
})
}
shinyApp(ui = ui, server = server)