如何通过更新和多重选择将多个输入构建到我闪亮的应用程序中?
How can I build multiple inputs into my shiny app with both updating and multiple selection?
我正在构建一个带有 datatable
的 flex dashboard
/ shiny
应用程序,并尝试构建两个输入作为此 datatable
的选择,带有 "All" 每个选择的选择。第一个问题是如何通过选择第一个选择"team"
来限制第二个选择"user"
?
然后,使用这些输入,我想将我的数据子集化为两个选择的任意组合,例如。 Team All,用户 "Darwin D" 将 return 一个单行数据表,其中包含他的姓名、团队和要添加的其他指标等。
以下 flex markdown 文档的所有代码:
---
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())
```
请注意,除非您提供可重现的示例,否则我无法对此进行测试,但按照这些思路应该可以。您的 server
中需要一个反应函数,其中包括一个子集步骤和一个 updateSelectInput
调用。每当反应被触发时,这将更新您 ui
中的输入。
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 ) )
)
})
因此,每当 input$team
发生变化时,我们都会根据该选择创建一个子集,并使用该子集来更新 user
输入字段。
您可能想使用 2 reactive
,第一个按团队过滤 data.frame,第二个按名称过滤第一个的结果:
---
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())
```
我正在构建一个带有 datatable
的 flex dashboard
/ shiny
应用程序,并尝试构建两个输入作为此 datatable
的选择,带有 "All" 每个选择的选择。第一个问题是如何通过选择第一个选择"team"
来限制第二个选择"user"
?
然后,使用这些输入,我想将我的数据子集化为两个选择的任意组合,例如。 Team All,用户 "Darwin D" 将 return 一个单行数据表,其中包含他的姓名、团队和要添加的其他指标等。
以下 flex markdown 文档的所有代码:
---
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())
```
请注意,除非您提供可重现的示例,否则我无法对此进行测试,但按照这些思路应该可以。您的 server
中需要一个反应函数,其中包括一个子集步骤和一个 updateSelectInput
调用。每当反应被触发时,这将更新您 ui
中的输入。
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 ) )
)
})
因此,每当 input$team
发生变化时,我们都会根据该选择创建一个子集,并使用该子集来更新 user
输入字段。
您可能想使用 2 reactive
,第一个按团队过滤 data.frame,第二个按名称过滤第一个的结果:
---
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())
```