vendredi 11 février 2022

Filtering data with selections and checkboxes in Flexdashboard

In a flexdashboard, I want to allow a selection by having selectInput() combined with a checkboxGroupInput(). But the data is not updated correctly and I cannot figure out what is wrong.

I used the toy example from this SO-example [LINK] and included a checkbox. It seems that the Checkbox is still ignored and messes up the other parts of the code.

It would be great if someone could adjust the code so that selection can be based on one of the three columns Team, Name or Year.

---
title: "example"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    runtime: shiny
---

```{r setup, include=FALSE}
library(DT)
library(shiny)

users <- data.frame(
  Name = c("Allan A","Barbara B","Charles C","Darwin D","Evelyn E","Frank F","Greg G","Hans H"),
  Team = c(1,2,3,3,2,1,2,2),
  Year = c(1999,1999,1999,2000,2000,2000,2003,2003), 
  stringsAsFactors = FALSE)
```

Inputs {.sidebar}
=======================================================================
### Input Variables

```{r global_input}

### selection Input based on columns Team and Name
selectInput("teaminput","Team",c("All", unique(users$Team)), selected="All")
selectInput("userinput","User Name", c("All", unique(users$Name) ), selected="All")

### Add Input from CheckboxGroup based on Year starting with all
checkboxGroupInput("yearinput", label = "Year",choices =  unique(users$Year), selected=unique(users$Year), inline = TRUE)


### Filter Team based on one of the three choices (Team, Name, Year)
teamFiltered <- reactive(users[input$teaminput=="All" | 
                                 users$Team==input$teaminput |
                                 users$Year ==input$yearinput,])

### Observe if userinput changes based on SELECTION
observe(updateSelectInput(session,"userinput", 
                          choices = c( unique(teamFiltered()$Name)), 
                          selected="All"))

### Observe if yearinput changes based on CHECK BOX
observe(updateCheckboxGroupInput(session,"yearinput", 
                                  choices =  teamFiltered()$Year,
                                  selected="All"))

```

Results
=======================================================================
### Intake Coordinator KPIs

```{r daily_table}
userFiltered <- reactive(teamFiltered()[input$userinput=="All" | 
                         teamFiltered()$Name==input$userinput | 
                         teamFiltered()$Year==input$yearinput,])

renderDataTable(userFiltered())
```



Aucun commentaire:

Enregistrer un commentaire