【问题标题】:R Shiny: How to hide tabPanel by a condition in serverR Shiny:如何通过服务器中的条件隐藏 tabPanel
【发布时间】:2018-10-08 07:46:30
【问题描述】:

我修改了here 中给出的答案,以使用 R 中的 Shiny 包编写一个非常简单的登录/注销系统。如果 USER$Logged 为 FALSE(即用户已登录),我想隐藏面板“B” out) 并在 USER$Logged 为 TRUE 时显示它(即用户已登录)。换句话说,一旦你运行代码,它不应该显示面板 B,直到用户正确输入用户名和密码。我尝试使用 conditionalPanel,但它没有隐藏面板 B。它目前一直在显示它,无论 USER$Logged 是什么。有人知道怎么解决吗?

library(shiny)
library(shinydashboard)
my_username <- "test"
my_password <- "abc"
shinyApp(
  shinyUI(
    navbarPage( tabPanel("A", uiOutput('loginpage')),
               tabPanel("B", uiOutput('page1'),conditionalPanel(condition = "output.cond1==TRUE"))
                            )
                            ),
  shinyServer(function(input, output, session) {
    USER <<- reactiveValues(Logged = FALSE)
    observe({
      if (USER$Logged == FALSE) {
        output$loginpage <- renderUI({
          box(title = "",textInput("userName", "Username"),
              passwordInput("passwd", "Password"),
              br(),
              actionButton("Login", "Log in"))})
      } else if (USER$Logged == TRUE) {
        output$loginpage <- renderUI({fluidPage(
          box(title = "",br(),br(),actionButton("logout", "Logout"))
        )

        })
      }
    })

    observeEvent(input$Login, {
      if (!is.null(input$Login)) {
        if (input$Login > 0) {
          Username <- isolate(input$userName)
          Password <- isolate(input$passwd)
          Id.username <- which(my_username == Username)
          Id.password <- which(my_password == Password)
          if (length(Id.username) > 0 & length(Id.password) > 0) {
            if (Id.username == Id.password) {
              USER$Logged <<- TRUE

            } 
          }
        } 
      }

    })

    observeEvent(input$logout, {
      USER$Logged <<- FALSE
    })

    output$cond1 = reactive({
      USER$Logged==TRUE
    })

                       }     ))

【问题讨论】:

    标签: r shiny tabpanel


    【解决方案1】:

    您可以使用shinyjs 包来显示/隐藏选项卡。为此,我在navbar 中添加了id,以便我们可以选择要显示/隐藏的选项卡。您可以在observe 中添加shinyjs::show/hide 以在用户登录或注销时显示或隐藏选项卡。最后要注意的是,您需要在 UI 中的某处调用 useShinyjs(),以便这些功能可以正常工作。我还对您的代码进行了一些小修改(例如,去掉 &lt;&lt;-。应用程序是:

    library(shinyjs)
    shinyApp(
      shinyUI(
        navbarPage( id = "navbar",
                    useShinyjs(),
                    tabPanel("A", uiOutput('loginpage')),
                    tabPanel("B", uiOutput('page1'),conditionalPanel(condition = "output.cond1==TRUE"))
        )
      ),
      shinyServer(function(input, output, session) {
        USER <- reactiveValues(Logged = FALSE)
    
        observe({
          if (USER$Logged == FALSE) {
            output$loginpage <- renderUI({
              box(title = "",textInput("userName", "Username"),
                  passwordInput("passwd", "Password"),
                  br(),
                  actionButton("Login", "Log in"))})
    
            shinyjs::hide(selector = "#navbar li a[data-value=B]")
    
          } else if (USER$Logged == TRUE) {
            output$loginpage <- renderUI({fluidPage(
              box(title = "",br(),br(),actionButton("logout", "Logout"))
            )})
    
            shinyjs::show(selector = "#navbar li a[data-value=B]")
            }
    
        })
    
        observeEvent(input$Login, {
              Id.username <- which(my_username == input$userName)
              Id.password <- which(my_password == input$passwd)
              if(Id.username & Id.password) USER$Logged <<- TRUE
        })
    
        observeEvent(input$logout, {
          USER$Logged <- FALSE
        })
        }))
    

    【讨论】:

    • 嗨,您的回答对我真的很有帮助,但是在很短的时间内 B 选项卡面板在被隐藏之前是可见的。有没有办法跳过这个?
    猜你喜欢
    • 2018-03-25
    • 1970-01-01
    • 2018-03-01
    • 1970-01-01
    • 2021-11-15
    • 2018-08-03
    • 2018-08-10
    • 2018-07-17
    • 2014-06-08
    相关资源
    最近更新 更多