【发布时间】: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
})
} ))
【问题讨论】: