Search code examples
rshinyshiny-server

Unique values for slider range R Shiny


I'm trying to select only unique values for my range slider in my Shiny App. I am able to do this using the SliderTextInput but I'm struggling to find a way to do this for the range slider. Please see the code below. Any suggestions?

#Example dataframe:

    df<- data.frame("ID" = c("001","001","001"), "date" = c("2020-07-01 01:00:00","2020-07-01 03:00:00","2020-07-01 06:00:00"))
    
    library(shiny)
    library(move)
    library(amt) 
    library(tibble)
    library(dplyr)
    library(htmltools)
    library(dygraphs)
    library(ggplot2)
    library(plotly)
    library(shinythemes)
    library(shinydashboard)
    library(datetime)
    library(shinyTime)
shinyServer(function(input, output, session) {
    
    observeEvent(input$selectVariable, {
        min<- min(as.POSIXct(df$date))
        max<- max(as.POSIXct(df$date))
        
        
        updateSliderTextInput(session, "month", choices = sort(unique(df$date)), selected = sort(unique(df$date)))

        updateSliderInput(session, "falltime", min = min, max = max, value =sort(unique(as.POSIXct(df$date))), timezone = "MST")
    })
})
    
    shinyUI(navbarPage(
        tabPanel("Analysis",
                 sidebarLayout(
                     sidebarPanel(width = 5,
                                  selectInput("selectVariable", "Select an ID:",
                                  choices =  unique(df$ID)),
                                  sliderTextInput("month",
                                                  "Date Range Correct:",
                                                  choices =  sort(unique(df$date))), #This slider works with the expected behavior but I need it to be a range slider
                                  sliderInput('falltime',"Slider Incorrect Date Range:", min = as.POSIXct("2020-01-01 00:00:00", tz = "MST"), max = as.POSIXct("2020-02-02 00:00:00", tz = "MST"),
                                              value = c(as.POSIXct("2020-01-01 00:00:00", tz = "MST"),as.POSIXct("2020-02-01 00:00:00", tz = "MST"))#, step =
                                  )), #Can't figure out how to make this slider select only unique values
                     mainPanel(h2("Uploaded Data")))
                 )
    
        ) 
    )
        

Solution

  • You can use sliderTextInput for that as well. It has choices argument which can take all the unique values that you want to show and selected argument which will show the first range selected by default.

    library(shiny)
    library(shinyWidgets)
    
    df<- data.frame("ID" = c("001","001","001"), "date" = as.POSIXct(c("2020-07-01 01:00:00","2020-07-01 03:00:00","2020-07-01 06:00:00")))
    df
    
    server <- function(input, output, session) {
    }
    
    ui <- navbarPage(
      tabPanel("Analysis",
               sidebarLayout(
                 sidebarPanel(width = 5,
                              selectInput("selectVariable", "Select an ID:",
                                          choices =  unique(df$ID)),
                              sliderTextInput("month",
                                              "Date Range Correct:",
                                              choices =  sort(unique(df$date))), 
                              sliderTextInput('falltime',"Slider Incorrect Date Range:", 
                                          choices =  unique(df$date), selected = range(df$date)
                              
                              )), 
                 mainPanel(h2("Uploaded Data")))
      )
      
    ) 
    shinyApp(ui, server)