Search code examples
rshinyflexdashboard

update slider range value based on selectInput value


I have a flexdashboard with a shiny app. I want to update the value of a sliderInput called "agerange" based on a selectInput value in "agecat". With the code below, I can get the lower value of the selected range to change from 15 to 20 when I select the age 20-24 category, but the upper value remains 99 and does not change to 20.

---
title: "test"
output: 
  flexdashboard::flex_dashboard:
    theme: bootstrap
runtime: shiny
---

```{r setup, include=FALSE}
library(flexdashboard)
library(tidyverse)
```

```{r global, include=FALSE}
  set.seed(1)
  dat <- data.frame(age = sample(15:99, 100, replace=TRUE),
                    y = runif(100))
```

Sidebar {.sidebar}
=====================================

```{r}
# age
  sliderInput("agerange", label = "Age", 
              min = 15, 
              max = 99, 
              value = c(15, 99),
              step=10)

# age category
  selectInput("agecat", label = "Age Category", 
    choices = list("All" = 1,
                   "15-19" = 2, 
                   "20-24" = 3), 
    selected = 1)

   observe({
        updateSliderInput(session, "agerange", 
                          value = ifelse(input$agecat==2, c(15,19),
                                  ifelse(input$agecat==3, c(20,24),
                                  input$agerange)))
      })

```

Page 1
=====================================

Column 
-----------------------------------------------------------------------

### Chart A

```{r}
renderPlot({
  dat %>%
    filter(age >= input$agerange[1] & age <= input$agerange[2]) %>%
    ggplot(., aes(y)) +
      geom_histogram()
})
```

Solution

  • The primary issue is with ifelse that was returning only one value (because of the condition size input$agecat) instead of two values. In the below code, I've created a new variable range with a default value and that changes based on the observeEvent

    ---
    title: "test"
    output: 
      flexdashboard::flex_dashboard:
      theme: bootstrap
    runtime: shiny
    ---
    
      ```{r setup, include=FALSE}
    library(flexdashboard)
    library(tidyverse)
    ```
    
    ```{r global, include=FALSE}
    set.seed(1)
    dat <- data.frame(age = sample(15:99, 100, replace=TRUE),
                      y = runif(100))
    ```
    
    Sidebar {.sidebar}
    =====================================
    
      ```{r}
    # age
    sliderInput("agerange", label = "Age", 
                min = 15, 
                max = 99, 
                value = c(15, 99),
                step=10)
    
    # age category
    selectInput("agecat", label = "Age Category", 
                choices = list("All" = 1,
                               "15-19" = 2, 
                               "20-24" = 3), 
                selected = 1)
    observeEvent(input$agecat,{
    
      range = c(15,99)
    
      if(input$agecat == 2) {
        range = c(15,19)
      } 
      else if(input$agecat == 3) {
        range = c(20,24)
      }
      else {
        input$agecat
      }
      updateSliderInput(session, "agerange", 
                        value = range,
                        min = min(range),
                        max = max(range),
                        step = 1)
    })
    
    ```
    
    Page 1
    =====================================
    
      Column 
    -----------------------------------------------------------------------
    
      ### Chart A
    
      ```{r}
    renderPlot({
      dat %>%
        filter(age >= input$agerange[1] & age <= input$agerange[2]) %>%
        ggplot(., aes(y)) +
        geom_histogram()
    })
    ```