根据selectInput值更新滑块范围值

时间:2018-09-06 09:24:37

标签: r shiny flexdashboard

我有一个带有闪亮应用程序的flexdashboard。我想基于“ agecat”中的selectInput值更新名为“ agerange”的sliderInput的value。使用下面的代码,当我选择20-24岁年龄段类别时,我可以将所选范围的下限值从15更改为20,但上限值仍为99,并且不会更改为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()
})
```

1 个答案:

答案 0 :(得分:1)

主要问题是static仅返回一个值(由于条件大小ifelse)而不是两个值。在下面的代码中,我创建了一个具有默认值的新变量input$agecat,该变量根据range

进行了更改
observeEvent