R 她很有光泽。如何使selectinput选项在子集数据时相互反应
我正在寻找一种方法,让用户根据多个变量的输入值(例如姓名和年龄)对数据进行子集,但一旦在一个输入中进行了选择,其他下拉列表应该是被动的,并且只提供与已选择的输入相对应的选择。我有从“名字”到“年龄”的工作方式,但我也希望它能以另一种方式工作,如果先选择“年龄”。我已经在下面发布了我的代码R 她很有光泽。如何使selectinput选项在子集数据时相互反应,r,shiny,subset,dropdown,reactive,R,Shiny,Subset,Dropdown,Reactive,我正在寻找一种方法,让用户根据多个变量的输入值(例如姓名和年龄)对数据进行子集,但一旦在一个输入中进行了选择,其他下拉列表应该是被动的,并且只提供与已选择的输入相对应的选择。我有从“名字”到“年龄”的工作方式,但我也希望它能以另一种方式工作,如果先选择“年龄”。我已经在下面发布了我的代码 l <- NULL; l$name <- c('b','e','d','b','b','d','e') l$age <- c(20,20,21,21,20,22,22) l <- as.
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)
l这是你想要的吗(如果不是,请告诉我,我会设法找到我可以帮助你的方法):
l这是你想要的吗(如果不是,请告诉我,我会设法找到我可以帮助你的方法):
l我终于得到了这个:
我想这就是你要找的?告诉我
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)
l我终于得到了这个:
我想这就是你要找的?告诉我
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)
l它正在创建正确的子集,但我希望下拉菜单中只有与已选择的变量相对应的选项。因此,如果选择“b”,年龄只能选择“20”和“21”,如果选择“21”,姓名只能选择“b”和“d”。好的,对不起,我错了。你试过使用observe()函数吗?我试过使用observe()来创建一个唯一的列表(sort(data3()$name))并将其用作下拉列表的选项,但它似乎不起作用,我想我找到了方法。检查我的最后一个答案它正在创建正确的子集,但是我希望下拉菜单中只有与已经选择的变量相对应的选项。因此,如果选择“b”,年龄只能选择“20”和“21”,如果选择“21”,姓名只能选择“b”和“d”。好的,对不起,我错了。你试过使用observe()函数吗?我试过使用observe()来创建一个唯一的列表(sort(data3()$name))并将其用作下拉列表的选项,但它似乎不起作用,我想我找到了方法。检查我的最后一个答案与你的问题相同与你的问题相同