提问人:Nate P 提问时间:1/27/2023 最后编辑:Nate P 更新时间:1/27/2023 访问量:51
有没有一种更简单/更整洁/更好的方法来创建多个相对动态的用户选择下拉列表?
Is there an easier/tidier/better way of creating multiple, relative dynamic user selection drop downs?
问:
我正在构建一个闪亮的仪表板,我希望用户可用的选择反映用户已经做出的选择。像这样:
第一个选择器:选项 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)
答:
1赞
YLC
1/27/2023
#1
尝试使用 conditionalPanel。我简化了您的代码,如下所示。基本上,我的建议是将代码的复杂性与条件面板的简单性相交换,但代价是创建更多的 SelectInput。
请注意类别如何根据数据集而变化。另请注意,当选择数据集 B 和类别 T 时,将显示统计和分层。您应该注意到有三个不同的 SelectInput(“categoryx”, ...)。最后,请注意,有一些副作用需要解决。如果您需要更多帮助,请伸出援手。
图书馆(闪亮) 图书馆(shinydashboard)
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(
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"))
)
),
仪表板Body() )
服务器 <- function(input, output, session) { }
shinyApp(ui,服务器)
评论
0赞
Nate P
1/28/2023
我顺便看了这些,但没有深入研究。这真是太棒了,欢迎建议!谢谢!我会和他们一起惹。
评论