根据闪亮仪表板中匹配的条件浏览 tabitem

Navigate through tabitems based on conditions matched in shiny dashboard

我有下面这个闪亮的应用程序,其中包含 3 个操作按钮。每个按钮都与某个选项卡相关联。我想做的是根据 Consent.

选项卡的内容构建以下架构

当第一次执行应用程序时,用户会进入 Welcome 选项卡。然后按下 Get started 操作按钮。

如果用户在 Consent 选项卡中输入 consent.name n 亲属 textInput() Name,则在按下 Get started 按钮之前,他将移至 Password 选项卡。

如果用户在没有在 Name 中输入文本的情况下按下 Get started 按钮,那么他将被移动到 Consent 选项卡,在 Name 中输入文本,按 Continue 然后移动到 Password 选项卡。

我想知道如何有条件地使用这些操作按钮来浏览选项卡。当然,用户可以通过单击顶部的操作按钮转到他希望的任何选项卡,但是当单击 Get started 按钮时会激活上述过程。

library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(shinyjs)
mytitle <- paste0("Life, Death & Statins")
dbHeader <- dashboardHeaderPlus(

)

shinyApp(
  ui = dashboardPagePlus(
    header = dbHeader,
    sidebar = dashboardSidebar(width = "0px",
                               sidebarMenu(id = "sidebar", # id important for updateTabItems
                                           menuItem("Welcome", tabName = "well", icon = icon("house")),
                                           menuItem("Consent", tabName = "conse", icon = icon("line-chart")),
                                           menuItem("Password", tabName = "pswd", icon = icon("house"))
                                           
                               )           ),
    body = dashboardBody(
      fluidRow(
        column(6,),
        column(1,
               tags$li(class = "dropdown", actionButton("well", "Welcome"))),
        column(1,
               tags$li(class = "dropdown", actionButton("conse", "Consent"))),
        column(1,
               tags$li(class = "dropdown", actionButton("pswd", "Password")))
        ),
      
      tabItems(
        tabItem("well",
                         actionButton("button", "Get started",style='padding:4px; font-size:140%')),
        tabItem("conse",
                
                tags$hr(),
                fluidRow(column(3,textInput("name", label = ("Name"), value = "consent.name"))),
                tags$hr(),
                fluidRow(column(3,actionButton('continue', "Continue",style='padding:4px; font-size:180%')))
        ),
        tabItem("pswd",
                passwordInput("pwd", "Enter the Database browser password")
                  )
        
      ),
      
      
      
    )
    
  ),
  server<-shinyServer(function(input, output,session) { 
    hide(selector = "body > div > header > nav > a")
    observeEvent(input$well, {
      updateTabItems(session, "sidebar", "well")
    })
    observeEvent(input$conse, {
      updateTabItems(session, "sidebar", "conse")
    })
    observeEvent(input$pswd, {
      updateTabItems(session, "sidebar", "pswd")
    })
    
    
  }
  )
)

一种方法是再添加两个 observeEvents,如下所示:

server<-shinyServer(function(input, output,session) { 
    hide(selector = "body > div > header > nav > a")
    
    observeEvent(input$well, {
      updateTabItems(session, "sidebar", "well")
    })
    
    observeEvent(input$conse, {
      updateTabItems(session, "sidebar", "conse")
    })
      
    observeEvent(input$pswd, {
      updateTabItems(session, "sidebar", "pswd")
    })
    
    observeEvent(input$button, {
      if (is.null(input$name) | nchar(gsub("[[:space:]]", "", input$name))==0 | input$name=="consent.name" ) {
        updateTabItems(session, "sidebar", "conse")
      }
      
      if (!is.null(input$name) & nchar(gsub("[[:space:]]", "", input$name))>0 & input$name!="consent.name" ){
        updateTabItems(session, "sidebar", "pswd")
      }
    })
    
    observeEvent(input$continue, {
      if (!is.null(input$name) & nchar(gsub("[[:space:]]", "", input$name))>0 & input$name!="consent.name" ){
        updateTabItems(session, "sidebar", "pswd")
      }
      
    })
    
  }
  )