code r dynamic shiny selection reactive

code - R cajas de selección de actualización en Shiny



shiny r html (1)

Tengo un problema en el trabajo. Estoy construyendo una aplicación brillante y tratando de subconjuntos de datos en función del valor de tres cuadros de selección. Mis datos tienen tres variables, Comisionado, Vecindad y Práctica. Lo que necesito es que no importa en qué casilla el usuario decida primero filtrar los datos, las otras dos actualizaciones solo muestran opciones relevantes. El valor predeterminado de la carga es mostrar todas las opciones en todos los cuadros.

Empecé pero no llegué rápido a ningún lado. El siguiente código muestra todas las opciones en Comisionado para cargar pero nada para las otras dos. Cuando se selecciona una opción en el cuadro Comisionado, se actualiza el cuadro Barrio y luego, al seleccionar un barrio, se actualiza el cuadro Práctica.

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)


He modificado tu código de servidor para lograr lo que quieres.

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") }) }

¡Espero eso ayude!