2017-06-15 95 views
0

我正在尋找一種方法讓用戶根據多個變量的輸入值(例如名稱和年齡)來子集數據,但是一旦在一個輸入中進行了選擇,其他下拉菜單應該是被動的,只能提供與已經選擇的輸入相對應的選項。我從「名字」到「年齡」工作,但如果首先選擇「年齡」,我也希望它以另一種方式工作。我已經在下面發佈了我的代碼。R閃亮。如何使選擇輸入選項在對數據進行子集化時互爲響應

l <- NULL; 
l$name <- c('b','e','d','b','b','d','e') 
l$age <- c(20,20,21,21,20,22,22) 
l <- as.data.frame(l) 
l <- as_data_frame(l) 
l$name <- as.character(l$name) 

server <- shinyServer(function(input,output){ 

assign('All Names',unique(sort(l$name))) 
assign("All Ages", unique(sort(l$age))) 
data1 <- reactive(l[which(l$name %in% if(exists(input$name)) 
{get(input$name)}else{input$name}),]) 

output$Box1 = renderUI(
if(is.null(input$name) || input$name == "All Names"){ 
selectInput("name", "Choose Name", choices=c(c("All Names"), 
unique(sort(l$name)))) 
}else{selectInput("name", "Choose Name", choices=c(input$name,c("All 
Names")))} 
) 

output$Box2 = renderUI(
    if(is.null(input$age) || input$age == "All Ages"){ 
    selectInput("age", "Choose Age", choices=c("All Ages", 
unique(sort(data1()$age)))) 
    }else{ selectInput("age", "Choose Age", choices=c(input$age, "All Ages"))} 
) 
output$table1 <- renderTable(data1()) 
output$text1 <- renderPrint(input$name) 
data2 <- reactive(data1()[which(data1()$age %in% if(exists(input$age)) 
{get(input$age)}else{input$age}),]) 
output$table2 <- renderTable(data2()) 

}) 

ui <-shinyUI(fluidPage(
uiOutput("Box1"), 
uiOutput("Box2") 
,tableOutput("table1"), 
textOutput("text1"), 
tableOutput("table2") 
)) 

shinyApp(ui,server) 

例如,當用戶選擇「B」爲「21」顯示爲「時代」選擇的名字,只有「20」和,但後來曾經的那些時期之一被點擊,我想「名稱」下拉列表中的選項會作出反應,並僅顯示選定年齡段的選項。

任何建議將非常感激!

+0

https://stackoverflow.com/questions/44570404/updating-filters-in-shiny-app同樣的問題比你 –

回答

1

它是你想要的(如果不告訴我,我會盡力找到我怎麼能幫助你):

l <- NULL 
l$name <- c('b','e','d','b','b','d','e') 
l$age <- c(20,20,21,21,20,22,22) 
l <- as.data.frame(l) 
l$name <- as.character(l$name) 
library(shiny) 


server <- shinyServer(function(input,output){ 



    data1 <- reactive({ 


    if(input$Box1 == "All" & input$Box2 == "All"){ 
    l 
    }else if (input$Box1 == "All" & input$Box2 != "All"){ 
    l[which(l$age == input$Box2),] 
    }else if (input$Box1 != "All" & input$Box2 == "All"){ 
     l[which(l$name == input$Box1),] 
    }else{ 
     l[which(l$name == input$Box1 & l$age==input$Box2),] 
    } 
    }) 


output$table1 <- renderPrint({ 
    data1()} 
) 

}) 

ui <-shinyUI(fluidPage(
    selectInput("Box1","Choose name :", choices = c('All',unique(l$name))), 
    selectInput("Box2","Choose age :", choices = c('All',unique(l$age))), 
    verbatimTextOutput("table1") 
)) 

shinyApp(ui,server) 
+0

它創建正確的子集,但我想在下拉菜單,只需要選擇,對應於已經選擇的變量。所以如果選擇了「b」,年齡只能選擇「20」和「21」,如果選擇「21」,名字只能選擇「b」和「d」。 – Jamie

+0

對不起,我的錯誤。你用observe()函數試過了嗎? – MBnnn

+0

我試過使用Observe()來創建一個唯一的列表(sort(data3()$ name)),並使用它作爲下拉列表的選項,但它似乎不起作用 – Jamie

0

我有finnaly這樣的:

我覺得這是什麼你在找什麼?告訴我 !

l <- NULL 
l$name <- c('b','e','d','b','b','d','e') 
l$age <- c(20,20,21,21,20,22,22) 
l <- as.data.frame(l) 
l$name <- as.character(l$name) 
l$age <- as.numeric(l$age) 
library(shiny) 

server <- shinyServer(function(input,output, session){ 

    data1 <- reactive({ 
    if(input$Box1 == "All"){ 
     l 
    }else{ 
     l[which(l$name == input$Box1),] 
    } 
    }) 

    data2 <- reactive({ 
    if (input$Box2 == "All"){ 
     l 
    }else{ 
     l[which(l$age == input$Box2),] 
    } 
    }) 

    observe({ 

    if(input$Box1 != "All"){ 
     updateSelectInput(session,"Box2","Choose an age", choices = c("All",unique(data1()$age))) 
    } 

    else if(input$Box2 != 'All'){ 
     updateSelectInput(session,"Box1","Choose a name", choices = c('All',unique(data2()$name))) 
    } 

    else if (input$Box1 == "All" & input$Box2 == "All"){ 
     updateSelectInput(session,"Box2","Choose an age", choices = c('All',unique(l$age))) 
     updateSelectInput(session,"Box1","Choose a name", choices = c('All',unique(l$name))) 
    } 
    }) 


    data3 <- reactive({ 
    if(input$Box2 == "All"){ 
     data1() 
    }else if (input$Box1 == "All"){ 
     data2() 
    }else if (input$Box2 == "All" & input$Box1 == "All"){ 
     l 
    } 
    else{ 
     l[which(l$age== input$Box2 & l$name == input$Box1),] 
    } 
    }) 

    output$table1 <- renderTable({ 
    data3() 
    }) 


}) 



ui <-shinyUI(fluidPage(
    selectInput("Box1","Choose a name", choices = c("All",unique(l$name))), 
    selectInput("Box2","Choose an age", choices = c("All",unique(l$age))), 
    tableOutput("table1") 
)) 

shinyApp(ui,server)