error in sequence selection and plotting - R Shiny

99 views Asked by At

I have data which includes date, temperature, and the elevation at which the measurement was recorded. I am trying to create a simple reactive plot using R Shiny, where the plot changes depending on the elevation bounds one is interested in. Once the bounds are defined, it takes the mean of these bounds for each day and plots them. Here is an example dataset (apologies for how long winded it is to generate this):

day <- seq.Date(as.Date("2016-01-01"),as.Date("2016-12-31"),by='day')
temperature_c <- runif(366,0,35)
sonde.elev <- 215  
df.1 <- data.frame(day,sonde.elev,temperature_c)
sonde.elev <- 220
df.2 <- data.frame(day,sonde.elev,temperature_c)
sonde.elev <- 225
df.3 <- data.frame(day,sonde.elev,temperature_c)
sonde.elev <- 230
df.4 <- data.frame(day,sonde.elev,temperature_c)
sonde.elev <- 235
df.5 <- data.frame(day,sonde.elev,temperature_c)
sonde.elev <- 240
df.6 <- data.frame(day,sonde.elev,temperature_c)
sonde.elev <- 245
df.7 <- data.frame(day,sonde.elev,temperature_c)
sonde.elev <- 250
df.8 <- data.frame(day,sonde.elev,temperature_c)
sonde.elev <- 255
df.9 <- data.frame(day,sonde.elev,temperature_c)
sonde.elev <- 260
df.10 <- data.frame(day,sonde.elev,temperature_c)
sonde.elev <- 265
df.11 <- data.frame(day,sonde.elev,temperature_c)
sonde.elev <- 270
df.12 <- data.frame(day,sonde.elev,temperature_c)
sonde.elev <- 275
df.13 <- data.frame(day,sonde.elev,temperature_c)
sonde.elev <- 280
df.14 <- data.frame(day,sonde.elev,temperature_c)
sonde.elev <- 285
df.15 <- data.frame(day,sonde.elev,temperature_c)
sonde.elev <- 290
df.16 <- data.frame(day,sonde.elev,temperature_c)
campbell_out <- rbind(df.1,df.2,df.3,df.4,df.5,df.6,df.7,df.8,df.9,df.10,df.11,df.12,df.13,df.14,df.15,df.16)
campbell_out <- campbell_out[order(campbell_out$day),] 

I have set up my plot to have a page wish sidebar and a main panel which contains the plot.

Here is my ui.R:

#===================
# ui.R
#===================

library(shiny)

shinyUI(pageWithSidebar(
  headerPanel("Stack Overflow Lough"),
sidebarPanel(
  dateRangeInput(inputId = "dateRange",
                 label = "Date Range",
                 start = "2016-01-01",
                 max = Sys.Date()
                 ),
  sliderInput(inputId = "minimumElevation",
              label="Feet (ASL) - Minimum",
              min = 215,
              max = 290,
              value = 215,
              step = 1),
  sliderInput(inputId = "maxmimumElevation",
              label="Feet (ASL) - Maximum",
              min = 215,
              max = 290,
              value = 290,
              step = 1)  
  ),
mainPanel(plotOutput("theGraph"))
 )
)

and here is my server.R:

#===================
# server.R
#===================
library(plyr)
    shinyServer(function(input,output){
      passData <- reactive({
        campbell_out <- campbell_out[campbell_out$day %in%
                                       seq.Date(input$dateRange[1],
                                                input$dateRange[2], by = "days"),]
        campbell_out <- campbell_out[campbell_out$sonde.elev %in%
                                       seq(as.numeric(input$minimumElevation),
                                       as.numeric(input$maximumElevation), by=0.000001),]
        campbell_out
      })

      output$theGraph <- renderPlot({
        graphData <- ddply(passData(),.(day),numcolwise(mean))
        theGraph <- plot(temperature_c~day,data=graphData, ylab = "Temperature (c)", xlab = "Time")
        print(theGraph)
      })
    })

When I run this in R Shiny, and choose the dates I'm interested in, I get the following error:

Warning: Error in seq.default: 'to' must be of length 1

When I run a similar script to sample the dataset based on specific dates and elevation, and then plot it, it works fine. So the issue appears to be with my Shiny code. I'd be grateful if someone could tell me where I'm going wrong.

1

There are 1 answers

0
Mike Wise On BEST ANSWER

Try adding these lines to your passData reactive (which handles reactive initialization warnings):

  passData <- reactive({
    req(input$dateRange[1])
    req(input$dateRange[2])
    req(input$minimumElevation)
    req(input$maximumElevation)
    campbell_out <- campbell_out[campbell_out$day %in%
                                   seq.Date(input$dateRange[1],
                                            input$dateRange[2],by = "days"),]
    print(as.numeric(input$minimumElevation))
    print(as.numeric(input$maximumElevation))
    campbell_out <- campbell_out[campbell_out$sonde.elev %in%
                                   seq(as.numeric(input$minimumElevation),
                                       as.numeric(input$maximumElevation),by = 0.000001),]
    campbell_out
  })

and correcting the mis-spelling of inputId = "maxmimumElevation" in your shinyUI function.

Works fine for me then.