我正在try 根据用户 Select 的单选按钮在我shiny 的仪表板的特定选项卡中生成信息.例如,如果此人 Select 了"18-29",我想将特定年龄的信息添加到"乳腺癌"标签和"宫颈癌"标签中.然而,它目前正在将特定年龄的信息添加到所有选项卡中,而不是指定的选项卡中.我不确定我哪里出错了,也不知道如何弥补.下面是我使用的代码的简化版本.
library(shiny)
library(shinydashboard)
library(shinyjs)
header <- dashboardHeader(
title = "Breast and Cervical Cancer: How to Help You"
)
sidebar <- dashboardSidebar(
sidebarMenu(
radioButtons("age", "Select Your Age Range",
choices = c("18-29", "30-39", "40-49", "50-59", "60+"),
selected = "18-29"),
menuItem("About the Tool", tabName = "about"),
menuItem("Breast Cancer", tabName = "breast"),
menuItem("Cervical Cancer", tabName = "cervical"),
menuItem("More Information", tabName = "info")
)
)
body <- dashboardBody(
# Creating the Tabs
tabItems(
tabItem("about",
h1("About the Tool"),
p("Here's some information about the tool"),
),
tabItem("breast",
h1("Breast Cancer"),
p("You can use the following resources to learn some more about breast"),
),
tabItem("cervical",
h1("Cervical Cancer"),
p("You can use the following resources to learn some more about cervical"),
),
tabItem("info",
h1("More Information"),
p("You can use the following resources to learn some more"),
),
uiOutput("dynamic_tabs")
)
)
# Adding Age-Specific Information to Two Tabs
server = function(input, output) {
output$dynamic_tabs <- renderUI({
selected_age <- input$age
# Cervical Cancer Tab
if (selected_age == "18-29") {
tab_content <- tabItem(tabName = "cervical",
h2("Okay, this works"))
} else if (selected_age == "30-39") {
tab_content <- tabItem(tabName = "cervical",
h2("Testing again"))
} else if (selected_age == "40-49") {
tab_content <- tabItem(tabName = "cervical",
h2("Keep testing"))
} else if (selected_age == "50-59"){
tab_content <- tabItem(tabName = "cervical",
h2("One more to go"))
} else if (selected_age == "60+") {
tab_content <- tabItem(tabName = "cervical",
h2("finally"))
}
# Breast Cancer Tab
if (selected_age == "18-29") {
tab_content <- tabItem(tabName = "Breast",
h2("You and Your Breasts"))
} else if (selected_age == "30-39") {
tab_content <- tabItem(tabName = "Breast",
h2("Start Having Discussions"))
} else if (selected_age == "40-49") {
tab_content <- tabItem(tabName = "Breast",
h2("Time for Your Mammogram!"))
} else if (selected_age == "50-59"){
tab_content <- tabItem(tabName = "Breast",
h2("It's Time to See Your OBGYN"))
} else if (selected_age == "60+") {
tab_content <- tabItem(tabName = "Breast",
h2("Continue Discussions and Get Checked"))
}
tab_content
})
}
shinyApp(ui = dashboardPage(header, sidebar, body), server)```