为什么我不能回到以前浏览过的页面?

时间:2019-09-26 13:59:01

标签: r shiny rstudio shinydashboard

我想知道为什么当我在闪亮的应用程序中打开一个页面(a1)然后又打开另一个页面(例如a2)时却无法回到{{ 1}}?

要重现我的示例,请按照以下步骤操作:

  1. 点击a1
  2. 点击a2
  3. 点击b1
  4. 点击c1
  5. 再次点击c8

我希望再次看到c1的内容(“文本2的示例”),但是再次单击c1似乎无济于事。

c1

1 个答案:

答案 0 :(得分:2)

您的其他菜单在sidebarMenu外部呈现,因此,选中它们时,它们的id不会记录在输入变量input$tabs中(应用程序正在监视该变量)。将菜单包含在sidebarMenu中可以使tabItems跟踪menuItems,但是也会中断反应,即,当input$tabs == 'uci_iden'时,其他菜单消失(因为input$tabs != 'identificacion' )。


实现所需行为的一种方法(可能不是最佳方法)是使用server通过renderUI渲染其他菜单。请注意,我们仍然必须在sidebarMenu中包含其他菜单,并通过input$tabs对其进行监视。为确保它们在input$tabs != 'identificacion'时停留在周围,我们可以在条件中包括它们的id。当identificationuni_idenadmision_iden均未选择时,我们将呈现一个空白的div


enter image description here

更新的代码:

library(shinydashboard)
library(shiny)

sidebar <- dashboardSidebar(
  tags$head(tags$style(
    HTML('.content-wrapper { height: 1500px !important;}')
  )),
  hr(),
  sidebarMenu(
    id = "tabs",
    menuItem(
      "a1",
      tabName = "principal",
      icon = icon("pagelines"),
      selected = TRUE
    ),
    menuItem(
      "a2",
      icon = icon("chart-bar"),
      menuSubItem("b1", tabName = "identificacion", icon = icon("angle-right")),
      menuSubItem("b2", tabName = "comunicacion", icon = icon("angle-right")),
      menuSubItem("b3", tabName = "medicamentos", icon = icon("angle-right")),
      menuSubItem("b4", tabName = "cirugias", icon = icon("angle-right")),
      menuSubItem("b5", tabName = "infecciones", icon = icon("angle-right")),
      menuSubItem("b6", tabName = "caidas", icon = icon("angle-right"))
    ),
    menuItem("a3", tabName = "procesos", icon = icon("chart-bar")),
    menuItem("a4", tabName = "tiempos", icon = icon("chart-bar")),
    menuItem("a5", tabName = "manual", icon = icon("mortar-board")),
    menuItem("a6", tabName = "acerca", icon = icon("question")),
  width = 285,
  hr(),
  uiOutput("more_menus")
  )
)

body <- dashboardBody(tabItems(
  tabItem(tabName = "principal",
          withMathJax(), ("example of text")),

  tabItem(tabName = "admision_iden", titlePanel("example1"), "example of text 2"),

  tabItem(tabName = "uci_iden", titlePanel("example 2"), "example of text 3")
))

ui <- dashboardPage(dashboardHeader(title = "Indic", titleWidth = 285),
                    sidebar,
                    body)

server <- function(input, output) {

  make_menus <- reactive({
    if (req(input$tabs) %in% c("identificacion", "admision_iden", "uci_iden")) {
      fluidRow(column(1),
               column(
                 10,
                 menuItem(
                   "c1",
                   tabName = "admision_iden",
                   icon = icon("chart-line"),
                   selected = FALSE
                 ),
                 menuItem(
                   "c8",
                   tabName = "uci_iden",
                   icon = icon("chart-line"),
                   selected = FALSE
                 )
               ))
    } else {
      div()
    }
  })
  output$more_menus <- renderUI({ make_menus() })
}

runApp(list(ui = ui, server = server))