2

I am building a flex dashboard / shiny app with a datatable and trying to build in two inputs as selections for this datatable with an "All" choice for each selection. First question is how can I limit the second selection "user" by the selection of the first choice "team"?

Then, using these inputs, I'd like to subset my data to any combination of the two selections ex. Team All, user "Darwin D" would return a single line datatable with his name, team and other metrics to be added etc.

All code for a flex markdown document below:

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

```{r setup, include=FALSE}
library(shiny)
library(shinydashboard)
library(flexdashboard)
library(magrittr)
library(feather)
library(anytime)
library(data.table)
library(DT)
library(datasets)


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)
users <- data.frame(Name,Team)
remove(Name,Team)
```



Inputs {.sidebar}
=======================================================================

### Input Variables

```{r global_input}
# input variable to call selection, name of field, selections/options variable
dateRangeInput('dateRange',
     label = 'Date range input: yyyy-mm-dd',
     start = Sys.Date() - 8,
     end = Sys.Date() - 1,
     min = "2013-01-01",
     max = Sys.Date() -1
   )

selectInput("teaminput","Team", c("All",unique(users$Team)))

observe({
    if( input$teaminput == "All" ) {
        subDT <- copy( users )
    } else {
        subDT <- users[ users$Team == input$teaminput, ]
    }

    updateSelectInput(
        "userinput",
        label = "User Name",
        choices = c( "All", unique(subDT$Name ) )
    )
})
```

### Intake Coordinator KPIs

```{r daily_table}
# reactive data object based on inputs above
daily_dt <- reactive({
  if(input$teaminput == "All"){
      subDT
}  else{
    subset(subDT$Team == input$teaminput)
}
  })

# render DT datatable object with sorts/search
renderDataTable(daily_dt())
```
1
  • Added a full code example. Commented Feb 10, 2017 at 17:12

2 Answers 2

2

You might want to use 2 reactive, the first one to filter the data.frame by Team, the second to filter the result of the first one by Name :

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

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

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), stringsAsFactors = FALSE)
```

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

```{r global_input}
selectInput("teaminput","Team",c("All", unique(users$Team)), selected="All")
selectInput("userinput","User Name", c("All", unique(users$Name) ), selected="All")

teamFiltered <- reactive(users[input$teaminput=="All" | users$Team==input$teaminput,])

observe(updateSelectInput(session,"userinput", choices = c("All", unique(teamFiltered()$Name)), selected="All"))
```

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

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

renderDataTable(userFiltered())
```
Sign up to request clarification or add additional context in comments.

Comments

0

Note I can't test this unless you provide a reproducible example, but something along these lines should work. You need a reactive function in your server, which includes a subsetting step, and an updateSelectInput call. This will update the input in your ui any time the reactive is triggered.

observe({
    if( input$team == "All" ) {
        subDT <- copy( DT )
    } else {
        subDT <- DT[ Team == input$team, ]
    }

    updateSelectInput(
        "user",
        label = "User Name",
        choices = c( "All", unique( subDT$Name ) )
    )

})

So any time input$team is changed, we create a subset based on that selection, and use that subset to update the user input field.

1 Comment

Added. I see where you are going with this - perfect. What about on the output side? Do you think the nested subframe would be best? The implementation I had tried to go for was a conditional selection of 4 outcomes: Both = all, Team = selected, Name = selected, Team and Name = selected. Does that make sense?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.