R更新Shiny中的选择框
R update selection boxes in Shiny
我在工作中遇到了问题。我正在构建一个闪亮的应用程序并尝试根据三个选择框的值对数据进行子集化。我的数据具有三个变量,Commissioner、Neighborhood 和 Practice。我需要的是,无论用户首先选择哪个框来过滤数据,其他两个更新为仅显示相关选项。加载时默认显示所有框中的所有选项。
我已经开始了,但进展不快。下面的代码显示了 Commissioner 在加载时的所有选择,但其他两个没有。当专员盒中选择选择时,邻里框更新,然后在选择附近时,练习盒更新。
library(shiny)
ui <- fluidPage(
fluidRow(
div(style="display: inline-block;vertical-align:top;",
uiOutput("Box1"),
uiOutput("Box2"),
uiOutput("Box3")
)
)
)
server <- function(input, output) {
data <- data.frame(Commissioner = c(rep("CommA", 6), rep("CommB", 6)),
Neighbourhood = c(rep("Nhood1", 2),
rep("Nhood2", 2),
rep("Nhood3", 2),
rep("Nhood4", 2),
rep("Nhood5", 2),
rep("Nhood6",2)),
Practice = c("Prac1", "Prac2", "Prac3", "Prac4", "Prac5",
"Prac6", "Prac7", "Prac8", "Prac9", "Prac10",
"Prac11", "Prac12")
)
output$Box1 = renderUI(
selectInput("commissioner",
"Select a Commissioner",
c("All",as.character(unique(data$Commissioner))),
"selectcommissioner")
)
output$Box2 = renderUI(
selectInput("neighbourhood",
"Select a Neighbourhood",
c("All", as.character(unique(data$Neighbourhood[which(data$Commissioner == input$commissioner)]))),
"selectnhood")
)
output$Box3 = renderUI(
selectInput("practice",
"Select a Practice",
c("All", as.character(unique(data$Practice[which(data$Neighbourhood == input$neighbourhood)]))),
"selectpractice")
)
}
shinyApp(ui = ui, server = server)
我已经修改了你的服务器代码来实现你想要的。
server <- function(input, output) {
data <- data.frame(Commissioner = c(rep("CommA", 6), rep("CommB", 6)),
Neighbourhood = c(rep("Nhood1", 2),
rep("Nhood2", 2),
rep("Nhood3", 2),
rep("Nhood4", 2),
rep("Nhood5", 2),
rep("Nhood6",2)),
Practice = c("Prac1", "Prac2", "Prac3", "Prac4", "Prac5",
"Prac6", "Prac7", "Prac8", "Prac9", "Prac10",
"Prac11", "Prac12")
)
output$Box1 = renderUI(
selectInput("commissioner",
"Select a Commissioner",
c("All",as.character(unique(data$Commissioner))),
"selectcommissioner")
)
output$Box2 = renderUI({
if(!is.null(input$commissioner))
{
if(input$commissioner == "All"){
opts1 <- c("All", as.character(unique(data$Neighbourhood)))
}else{
opts1 <- c("All",as.character(unique(data$Neighbourhood[which(data$Commissioner==input$commissioner)])))
}
}
selectInput("neighbourhood",
"Select a Neighbourhood",
opts1,
"selectnhood")
})
output$Box3 = renderUI({
if(!is.null(input$neighbourhood))
{
if(input$neighbourhood == "All"){
opts2 <- c("All", as.character(unique(data$Practice)))
}else{
opts2 <- c("All", as.character(unique(data$Practice[which(data$Neighbourhood == input$neighbourhood)])))
}
}
selectInput("practice",
"Select a Practice",
opts2,
"selectpractice")
})
}
希望对您有所帮助!
我在工作中遇到了问题。我正在构建一个闪亮的应用程序并尝试根据三个选择框的值对数据进行子集化。我的数据具有三个变量,Commissioner、Neighborhood 和 Practice。我需要的是,无论用户首先选择哪个框来过滤数据,其他两个更新为仅显示相关选项。加载时默认显示所有框中的所有选项。
我已经开始了,但进展不快。下面的代码显示了 Commissioner 在加载时的所有选择,但其他两个没有。当专员盒中选择选择时,邻里框更新,然后在选择附近时,练习盒更新。
library(shiny)
ui <- fluidPage(
fluidRow(
div(style="display: inline-block;vertical-align:top;",
uiOutput("Box1"),
uiOutput("Box2"),
uiOutput("Box3")
)
)
)
server <- function(input, output) {
data <- data.frame(Commissioner = c(rep("CommA", 6), rep("CommB", 6)),
Neighbourhood = c(rep("Nhood1", 2),
rep("Nhood2", 2),
rep("Nhood3", 2),
rep("Nhood4", 2),
rep("Nhood5", 2),
rep("Nhood6",2)),
Practice = c("Prac1", "Prac2", "Prac3", "Prac4", "Prac5",
"Prac6", "Prac7", "Prac8", "Prac9", "Prac10",
"Prac11", "Prac12")
)
output$Box1 = renderUI(
selectInput("commissioner",
"Select a Commissioner",
c("All",as.character(unique(data$Commissioner))),
"selectcommissioner")
)
output$Box2 = renderUI(
selectInput("neighbourhood",
"Select a Neighbourhood",
c("All", as.character(unique(data$Neighbourhood[which(data$Commissioner == input$commissioner)]))),
"selectnhood")
)
output$Box3 = renderUI(
selectInput("practice",
"Select a Practice",
c("All", as.character(unique(data$Practice[which(data$Neighbourhood == input$neighbourhood)]))),
"selectpractice")
)
}
shinyApp(ui = ui, server = server)
我已经修改了你的服务器代码来实现你想要的。
server <- function(input, output) {
data <- data.frame(Commissioner = c(rep("CommA", 6), rep("CommB", 6)),
Neighbourhood = c(rep("Nhood1", 2),
rep("Nhood2", 2),
rep("Nhood3", 2),
rep("Nhood4", 2),
rep("Nhood5", 2),
rep("Nhood6",2)),
Practice = c("Prac1", "Prac2", "Prac3", "Prac4", "Prac5",
"Prac6", "Prac7", "Prac8", "Prac9", "Prac10",
"Prac11", "Prac12")
)
output$Box1 = renderUI(
selectInput("commissioner",
"Select a Commissioner",
c("All",as.character(unique(data$Commissioner))),
"selectcommissioner")
)
output$Box2 = renderUI({
if(!is.null(input$commissioner))
{
if(input$commissioner == "All"){
opts1 <- c("All", as.character(unique(data$Neighbourhood)))
}else{
opts1 <- c("All",as.character(unique(data$Neighbourhood[which(data$Commissioner==input$commissioner)])))
}
}
selectInput("neighbourhood",
"Select a Neighbourhood",
opts1,
"selectnhood")
})
output$Box3 = renderUI({
if(!is.null(input$neighbourhood))
{
if(input$neighbourhood == "All"){
opts2 <- c("All", as.character(unique(data$Practice)))
}else{
opts2 <- c("All", as.character(unique(data$Practice[which(data$Neighbourhood == input$neighbourhood)])))
}
}
selectInput("practice",
"Select a Practice",
opts2,
"selectpractice")
})
}
希望对您有所帮助!