Home > front end >  sliderInput `min`, `max` and `value` NULL problem in R Shiny App
sliderInput `min`, `max` and `value` NULL problem in R Shiny App

Time:02-08

I am trying to create a Time Series app in R-Shiny which shows data points on a Leaflet map between a selected Weekly time range via sliderInput. But sliderInput() is returning the following error:

 Error: In sliderInput(): `min`, `max`, and `value` cannot be NULL, NA, or empty.

How can this error be fixed?

Code

    # Time Series Map App v.1.1
    
    library(shiny)
    library(tidyverse)
    library(leaflet)
    
    # Create sample data
    Date = c("2014-04-08", "2014-06-04", "2014-04-30",
             "2014-05-30", "2014-05-01")
    lat = as.numeric(c("45.53814", "45.51076", "45.43560", "45.54332",
                       "45.52234"))
    lon = as.numeric(c("-73.63672", "-73.61029", "-73.60100",
                       "-73.56000 ", "-73.59022"))
    id = as.numeric(c("1", "2", "3", "4", "5"))
    
    # Create a df from the above columns
    df = data.frame(id, lat, lon, Date)
    df$Year = lubridate::year(df$Date)
    df$Month = lubridate::month(df$Date, label = TRUE, abbr=FALSE)
    df$Week = lubridate::week(df$Date)
    df$Date = as.Date(df$Date)
    
    
    ui = fluidPage(
        titlePanel("Time Series Map"),
        sidebarLayout(
            sidebarPanel(
                selectInput("Input_1", label = "Please select an ID", choices = unique(df$id)),
                
                # sliderInput("Input_2", label = "Select Year", 
                #             min = min(fruit$year), max = max(fruit$year), 
                #             value = median(fruit$year), step = 1),
                
                sliderInput("Input_2", label = "Please select a weekly range", 
                            min = min(df$Week), max = max(df$Week), 
                            value = unique(df$Week), step = 1)
            ),
            mainPanel(
                div(
                    class = "leaflet-full-screen",
                    leafletOutput("Time_Series_Map"))),
        ))
    
    server = function(input, output) {
        
        dataset = reactive({
            return(subset(df, (id   ==  input$Input_1 & 
                                      #year   ==  input$codeInput2 & 
                                   Week >= input$Input_3[1] && month <= input$Input_3[2])))
        })
    
    output$Time_Series_Map = leaflet(df) %>% addTiles() %>%
        addTiles(group = "OSM (default)") %>%
        addProviderTiles(provider = "Esri.WorldStreetMap",group = "World StreetMap") %>%
        addProviderTiles(provider = "Esri.WorldImagery",group = "World Imagery") %>%
        # addProviderTiles(provider = "NASAGIBS.ViirsEarthAtNight2012",group = "Nighttime Imagery") %>%
        addMarkers(lng = ~lon, lat = ~lat, popup = df$id, clusterOptions = markerClusterOptions()) %>%
        addLayersControl(
            baseGroups = c("OSM (default)","World StreetMap", "World Imagery"),
            options = layersControlOptions(collapsed = FALSE)
        )
}

shinyApp(ui = ui, server = server)

CodePudding user response:

Seems that shiny::sliderInput expects explicit values instead of R expressions. However, you can set dummy values for e.g. max and min and then update them with the real data at the very beginning of the server function. Here is a more minimal example:

library(shiny)

# Create sample data
Date <- c(
  "2014-04-08", "2014-06-04", "2014-04-30",
  "2014-05-30", "2014-05-01"
)
id <- as.numeric(c("1", "2", "3", "4", "5"))

# Create a df from the above columns
df <- data.frame(id, lat, lon, Date)
df$Year <- lubridate::year(df$Date)
df$Month <- lubridate::month(df$Date, label = TRUE, abbr = FALSE)
df$Week <- lubridate::week(df$Date)
df$Date <- as.Date(df$Date)


ui <- fluidPage(
  sliderInput("Input_2",
    label = "Please select a weekly range",
    step = 1,
    # init with dummy values
    min = 0, max = 1,
    value = 1
  )
)

server <- function(input, output, session) {
  updateSliderInput(session, "Input_2",
    min = min(df$Week), max = max(df$Week),
    value = unique(df$Week)
  )
}

shinyApp(ui = ui, server = server)

enter image description here

  •  Tags:  
  • Related