R更新Shiny中的选择框 [英] R update selection boxes in Shiny

查看:83
本文介绍了R更新Shiny中的选择框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在工作中遇到问题.我正在构建一个Shiny应用程序,并尝试基于三个选择框的值对数据进行子集化.我的数据包含三个变量,专员,邻居和实践.我需要的是,无论用户首先选择在哪个框上过滤数据,其他两个框都更新为仅显示相关选择.加载时的默认设置是在所有框中显示所有选择.

I have a problem at work. I am building a Shiny app and trying to subset data based on the value of three selection boxes. My data has three variables, Commissioner, Neighbourhood and Practice. What I need is that no matter which box the user chooses first to filter the data on, the other two update to only show relevant choices. The default on loading is to show all choices in all boxes.

我已经开始了,但是很快就没了.下面的代码显示了Commissioner中关于加载的所有选择,而其他两个则没有显示.在专员"框中选择一个选项后,邻居"框将更新,然后在选择一个邻居时,实践"框将更新.

I've made a start but getting no where fast. The code below shows all the choices in Commissioner on loading but nothing for the other two. When a choice is selected in the Commissioner box the Neighbourhood box updates and then when selecting a neighbourhood the Practice box updates.

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)

推荐答案

我已修改您的服务器代码以实现所需的功能.

I have modified your server code to achieve what you want.

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

  })


}

希望有帮助!

这篇关于R更新Shiny中的选择框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆