R语言 有没有更简单/更整洁/更好的方法来创建多个相对动态的用户选择下拉唐斯?

mfpqipee  于 2023-01-28  发布在  其他
关注(0)|答案(1)|浏览(106)

我正在构建一个闪亮的 Jmeter 板,我希望用户可用的选择能够反映出用户已经做出的选择。
第一选择器:选项A或选项B
第二选择器:

if Option A: Choice A, Choice B, Choice C

 if Option B: Choice D, Choice E, Choice F

等等。
我想知道是否有比我做的更简单、更简洁的方法(即嵌套的if else调用)。
我是这样做的(这段代码并不真正依赖于任何数据--您应该能够将其插入控制台并运行它--减去路径的东西)。

library(shiny)
library(shinydashboard)
library(tidyverse)

################################################################################
## Global variables

start_year <- 2016
end_year <- 2021
afh <- NULL
alrc <- NULL

################################################################################
## Define UI

ui <- dashboardPage(
  
  ## General formatting
  skin = "black", 
  
  ## Title

  ## Header
  dashboardHeader(),
  
  ## Sidebar and inputs
  dashboardSidebar(
    ## formatting
    tags$style(type = "text/css", ".irs-grid-pol.small {height: 0px;}"),
    
    ## pane title
    selectInput("dataset", "Dataset", 
                c("-", 
                  "Adult Foster Home Resident and Community Characteristics", 
                  "Assisted Living/Residential Care Community Characteristics")
                ), 
    selectInput("category", "Category", "*Please select a dataset*"), 
    selectInput("statistic", "Statistic", "*Please select a category*"), 
    selectInput("stratification", "Stratification", c("None", "Region", "County")), 
    sliderInput(
      inputId = "year",
      label = "Year Range",
      value = c(start_year, end_year),
      min = start_year,
      max = end_year,
      step = 1,
      sep = ""
    )
  ),
  
  ## Body and outputs
  dashboardBody()
)

################################################################################
## Define Server components

server <- function(input, output, session) {
  
  ## Set the dynamic options for the Category dropdown selector
  observeEvent(input$dataset, {
    if (input$dataset == "-") {
      freezeReactiveValue(input, "category")
      updateSelectInput(inputId = "category",
                        choices = c("*Please select a dataset*"))
    } else
      if (input$dataset == "Adult Foster Home Resident and Community Characteristics") {
        freezeReactiveValue(input, "category")
        updateSelectInput(
          inputId = "category",
          choices = c(
            "*Please select a category*",
            "Homes",
            "Owners",
            "Staff",
            "Finances",
            "Residents",
            "COVID-19"
          )
        )
      } else
        if (input$dataset == "Assisted Living/Residential Care Community Characteristics") {
          freezeReactiveValue(input, "category")
          updateSelectInput(
            inputId = "category",
            choices = c(
              "*Please select a category*",
              "Community",
              "Staff",
              "Residents",
              "COVID-19"
            )
          )
        }
  })
  
  ## Set the dynamic options for the Statistic dropdown selector
  observeEvent(input$category, {
    if (input$category == "*Please select a dataset*") {
      freezeReactiveValue(input, "statistic")
      updateSelectInput(inputId = "statistic",
                        choices = c("*Please select a category*"))
    } else
      if (input$dataset == "Adult Foster Home Resident and Community Characteristics" &
          input$category == "Homes") {
        freezeReactiveValue(input, "statistic")
        updateSelectInput(
          inputId = "statistic",
          choices = c(
            "*Please select a statistic*",
            "AFH: Number",
            "AFH: Licensed Capacity",
            "AFH: Years of Operation"
          )
        )
      } else
        if (input$dataset == "Adult Foster Home Resident and Community Characteristics" &
            input$category == "Owners") {
          freezeReactiveValue(input, "statistic")
          updateSelectInput(
            inputId = "statistic",
            choices = c(
              "*Please select a statistic*",
              "AFH: Future Plans"
            )
          )
        } else
          if (input$dataset == "Adult Foster Home Resident and Community Characteristics" &
              input$category == "Staff") {
            freezeReactiveValue(input, "statistic")
            updateSelectInput(
              inputId = "statistic",
              choices = c(
                "*Please select a statistic*",
                "AFH: Tenure", 
                "AFH: Absenteeism", 
                "AFH: Contract Staff"
              )
            )
          } else
            if (input$dataset == "Adult Foster Home Resident and Community Characteristics" &
                input$category == "Finances") {
              freezeReactiveValue(input, "statistic")
              updateSelectInput(
                inputId = "statistic",
                choices = c(
                  "*Please select a statistic*",
                  "AFH: Medicaid Acceptance and Use", 
                  "AFH: Medicaid Reimbursement Rates", 
                  "AFH: Private Pay Rates by Region", 
                  "AFH: Additional Private-Pay Services and Charges"
                )
              )
            } else
              if (input$dataset == "Adult Foster Home Resident and Community Characteristics" &
                  input$category == "Residents") {
                freezeReactiveValue(input, "statistic")
                updateSelectInput(
                  inputId = "statistic",
                  choices = c(
                    "*Please select a statistic*",
                    "AFH: Demographics", 
                    "AFH: Move-In and Move-Out Locations", 
                    "AFH: Length of Stay", 
                    "AFH: Personal Care Services", 
                    "AFH: Assistance from Two Staff and Nighttime Care", 
                    "AFH: Visits and Assistance from Family Members and Friends", 
                    "AFH: Resident Health Conditions and Falls", 
                    "AFH: Health Service and Medication Use"
                  )
                )
              } else
                if (input$dataset == "Adult Foster Home Resident and Community Characteristics" & input$category == "COVID-19") {
                  freezeReactiveValue(input, "statistic")
                  updateSelectInput(
                    inputId = "statistic",
                    choices = c(
                      "*Please select a statistic*",
                      "AFH: Supports & Challenges"
                    )
                  )
                } else
                  if (input$dataset == "Assisted Living/Residential Care Community Characteristics" & input$category == "Community") {
                    freezeReactiveValue(input, "statistic")
                    updateSelectInput(
                      inputId = "statistic",
                      choices = c(
                        "*Please select a statistic*",
                        "ALRC: AL/RC/MC Supply Across Oregon", 
                        "ALRC: Ownership, Chain Affiliation, and Multi-License Settings", 
                        "ALRC: Occupancy Rates", 
                        "ALRC: Units and Room Sharing", 
                        "ALRC: Medicaid Acceptance, Medicaid Reimbursement, and Payer Sources", 
                        "ALRC: Private Pay Charges", 
                        "ALRC: Estimated Industry Charges", 
                        "ALRC: Use of Electronic Health Records", 
                        "ALRC: Additional Services"
                      )
                    )
                  } else
                    if (input$dataset == "Assisted Living/Residential Care Community Characteristics" & input$category == "Staff") {
                      freezeReactiveValue(input, "statistic")
                      updateSelectInput(
                        inputId = "statistic",
                        choices = c(
                          "*Please select a statistic*",
                          "ALRC: Care-Related Staff Employed Full-Time and Part-Time", 
                          "ALRC: Staff to Resident Ratios", 
                          "ALRC: Staffing Levels", 
                          "ALRC: Current Job Openings", 
                          "ALRC: Unplanned Staff Absences and Outside Service Provider Use", 
                          "ALRC: Recent Turnover and Current Staff Tenure"
                        )
                      )
                    } else
                      if (input$dataset == "Assisted Living/Residential Care Community Characteristics" & input$category == "Residents") {
                        freezeReactiveValue(input, "statistic")
                        updateSelectInput(
                          inputId = "statistic",
                          choices = c(
                            "*Please select a statistic*",
                            "ALRC: Demographics", 
                            "ALRC: Move-In, Move-Out, and Length of Stay", 
                            "ALRC: Length of Stay Among Residents Who Moved", 
                            "ALRC: Hospice Use Among Residents Who Died", 
                            "ALRC: Assistance with Personal Care", 
                            "ALRC: Health Conditions", 
                            "ALRC: Significant Change in Condition", 
                            "ALRC: Falls & Fall-Related Injuries", 
                            "ALRC: Health Service Use", 
                            "ALRC: Medication Use"
                          )
                        )
                      } else
                        if (input$dataset == "Assisted Living/Residential Care Community Characteristics" & input$category == "COVID-19") {
                          freezeReactiveValue(input, "statistic")
                          updateSelectInput(
                            inputId = "statistic",
                            choices = c(
                              "*Please select a statistic*",
                              "ALRC: Administrator Experiences"
                            )
                          )
                        } 
  })

################################################################################
## Run Application

shinyApp(ui, server)
xdnvmnnf

xdnvmnnf1#

尝试使用conditionalPanel。我如下简化了您的代码。基本上,我的建议是以创建更多SelectInput为代价,用条件面板的简单性来交换代码的复杂性。
请注意类别如何根据数据集而变化。还请注意,当选择数据集B和类别T时,会出现统计和分层。您应该注意到有三个不同的SelectInput("categoryx",...)。最后,请注意需要解决一些副作用。如果需要更多帮助,请联系我们。
存储库(shiny)存储库(shinydashboard)
ui〈- Jmeter 板页面(
Jmeter 板标题(),
Jmeter 板边栏(

selectInput("dataset", "Dataset", c("-", "A", "B")),

conditionalPanel(
  condition = "input.dataset == '-'",
  selectInput("category", "Category", "*Please select a dataset*")
),

conditionalPanel(
  condition = "input.dataset == 'A'",
  selectInput("category1", "Category", choices=c("H", "O", "S"))
),

conditionalPanel(
  condition = "input.dataset == 'B'",
  selectInput("category2", "Category", choices=c("C", "T", "R"))
),

conditionalPanel(
  condition = "input.category2 == 'T'",
  selectInput("statistic", "Statistic", c("*Please select a category*", "X", "Y", "Z")), 
  selectInput("stratification", "Stratification", c("None", "Region", "County")) 
)

),
Jmeter 板正文())
服务器〈-函数(输入、输出、会话){}
shinyApp(用户界面,服务器)

相关问题