我正在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)```

推荐答案

欢迎罗德里格斯,

您的代码的一个问题是您使用的是tabItem,其中只需要h2.(在用户界面中已经调用了表项"乳房"和"颈部",不需要在renderUI中调用它们).因此,只需使用if Else逻辑来定义h2内容.第二个uiOutput应该在UI中相应的表项下调用(因此为BC和CC分别创建一个动态UI).

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"),
            uiOutput("dynamic_tabs_BC"),
    ),
    tabItem("cervical",
            h1("Cervical Cancer"),
            p("You can use the following resources to learn some more about cervical"),
            uiOutput("dynamic_tabs_CC"),
            
    ),
    tabItem("info",
            h1("More Information"),
            p("You can use the following resources to learn some more"),
    )
  )
)
# Adding Age-Specific Information to Two Tabs
server = function(input, output) {
  output$dynamic_tabs_CC <- renderUI({
    selected_age <- input$age
    # Cervical Cancer Tab
    if (selected_age == "18-29") {
      tab_content <- h2("Okay, this works")
    } else if (selected_age == "30-39") {
      tab_content <- h2("Testing again")
    } else if (selected_age == "40-49") {
      tab_content <- h2("Keep testing")
    } else if (selected_age == "50-59"){
      tab_content <- h2("One more to go")
    } else if (selected_age == "60+") {
      tab_content <- h2("finally")
    }
    tab_content
  })
  output$dynamic_tabs_BC <- renderUI({
    selected_age <- input$age
    if (selected_age == "18-29") {
      tab_content <- h2("You and Your Breasts")
    } else if (selected_age == "30-39") {
      tab_content <- h2("Start Having Discussions")
    } else if (selected_age == "40-49") {
      tab_content <- h2("Time for Your Mammogram!")
    } else if (selected_age == "50-59"){
      tab_content <- h2("It's Time to See Your OBGYN")
    } else if (selected_age == "60+") {
      tab_content <- h2("Continue Discussions and Get Checked")
    }
    tab_content
  })
}

shinyApp(ui = dashboardPage(header, sidebar, body), server)

这是一个接近于您已有的解决方案,然而,我建议go 掉所有If Else的东西,使用带有索引的数据帧来呈现H2‘S.大概是这样的事情:

headers<-data.frame(
  age= c("18-29","30-39","40-49","50-59","60+"),
  CC = c("Okay, this works","Testing again","Keep testing","One more to go","finally"),
  BC = c("You and Your Breasts","Start Having Discussions","Time for Your Mammogram!","It's Time to See Your OBGYN","Continue Discussions and Get Checked")
  )

server = function(input,output){
  selected_age <- reactive({input$age})
  output$dynamic_tabs_CC <- renderUI({
    h2(headers[headers$age == selected_age(),]$CC)
  })
  output$dynamic_tabs_BC <- renderUI({
    h2(headers[headers$age == selected_age(),]$BC)
  })
}

R相关问答推荐

IQR()和stats之间四分位距计算的差异::分位数()在R和' ggpubr '

使用lapply的重新定位功能

如何在R中正确对齐放射状图中的文本

R Lubridate:舍入/快照日期时间到一天中最近的任意时间?

R中的子集文件—读取文件名索引为4位数字序列,例如0001到4000,而不是1到4000)

使用外部文件分配变量名及其值

在嵌套列表中查找元素路径的最佳方法

计算满足R中条件的连续列

您是否可以将组添加到堆叠的柱状图

如何在R中平滑地绘制线图(不拟合)?

按组计算列中1出现的间隔年数

如何使这些react 表对象相互独立?

按组内中位数分类

为什么在写入CSV文件时Purrr::Pwalk不起作用

按组和连续id计算日期差

计算来自单独分组的分幅的值的百分位数

如果满足条件,则替换列的前一个值和后续值

如何创建一个由一个连续变量和一个因素变量组成的复杂方框图?

如何在R中添加标识连续日期的新列

打印的.txt文件,将值显示为&Quot;Num&Quot;而不是值