2017-02-09 39 views
1

我正在構建一個flex dashboard/shiny應用程序datatable並嘗試構建兩個輸入作爲此datatable的選擇,每個選擇都有一個「全部」選項。第一個問題是我如何通過選擇第一個選項"team"來限制第二個選擇"user"如何使用更新和多重選擇在我的閃亮應用中創建多個輸入?

然後,使用這些輸入,我想將我的數據分爲任意兩個選擇組合。管理團隊,用戶「達爾文d」將返回單行數據表以他的名字,團隊和其他指標要添加等

低於柔性降價文件中的所有代碼:

--- 
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()) 
``` 
+0

增加了一個完整的代碼示例。 – gscott

回答

1

您可能需要使用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()) 
``` 
0

注意我可以除非你提供了一個可重現的例子,否則不會對此進行測試,但沿着這些方向的東西應該有效。您需要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輸入字段。

+0

已添加。我明白你要去哪裏 - 完美。在輸出端怎麼樣?你認爲嵌套子幀是最好的嗎?我曾嘗試過的實現是4個結果的條件選擇:Both = all,Team = selected,Name = selected,Team和Name = selected。那有意義嗎? – gscott