【问题标题】:Why logout button does not work -shiny R为什么注销按钮不起作用 -shiny R
【发布时间】:2018-01-10 15:29:04
【问题描述】:

注销按钮(actionbutton)在以下脚本中不起作用。我需要知道为什么它不起作用?以及如何解决?

仅供参考:当我从代码中取出USER$Logged <- FALSE 时,按钮可以工作,但我无法再登录,因为Logged 仍然是TRUE

欣赏!

用户界面

ui2 <- function(){
        tagList(tabPanel(""),
        pageWithSidebar(
        headerPanel(
            ""
        ),
        sidebarPanel
        (
            actionButton("logout", "Logout")        
        ),
            mainPanel(tableOutput('aaa'))
        )
    )   
}


ui1 <- function(){
    tagList(h2("Hello", align = "center"),
    div(id = "login",
        wellPanel(textInput("userName", "Username"),
                  passwordInput("passwd", "Password"),
                  br(),actionButton("Login", "Log in"),
  )
}

服务器

server = (function(input, output,session) {

USER <- reactiveValues(Logged=FALSE)
observe({ 
    if (USER$Logged == FALSE) {
      if (!is.null(input$Login)) {
        if (input$Login > 0) {
            Username <- isolate(input$userName)
            Password <- isolate(input$passwd)
            query <- sprintf({"
              SELECT rowid 
              FROM users 
              WHERE username='%s' and password ='%s'"}, 
                           Username, Password, serialize=F) 
            db   <- RSQLite::dbConnect(RSQLite::SQLite(), dbname="db.sqlite")
            user <- RSQLite::dbGetQuery(db, query) 
            RSQLite::dbDisconnect(db)
            if ( length(user$rowid)==1 ) {
              USER$Logged <- TRUE
          }
        } 
      }
    }    
})
observe({
    if (USER$Logged == FALSE)
    {
      output$page <- renderUI({div(class="outer",do.call(bootstrapPage,c("",ui1())))})
    }
    if (USER$Logged == TRUE) 
    {
      output$page <- renderUI({div(class="outer",do.call(navbarPage,c(inverse=TRUE,title = paste("Welcome", isolate(input$userName)," !"),ui2())))})
      print(ui)
    }
})
observeEvent(input$logout, {
    output$page <- renderUI({div(class="outer",do.call(bootstrapPage,c("",ui1())))})
    USER$Logged <- FALSE   
})

})

生成小型数据库:

library(shiny)
library(RSQLite)
setwd("E:/shiny/Correct")
db <- dbConnect(SQLite(), dbname="db.sqlite")
dbSendQuery(conn = db,"CREATE TABLE users (username TEXT, password TEXT, email TEXT)")
dbSendQuery(db, "INSERT INTO users ( username, password, email) VALUES ( 'ester', 'silva', 'abc@gmail.com');")

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    它不起作用,因为您不能只在 ui 对象之间进行交换。您可以做的是在ui 内部创建一个uiOutput,并在运行时在不同的用户界面之间切换。

    这是一个简单的例子。您可以添加自己的登录逻辑等。

    library(shiny)
    
    ui <- fluidPage(
      uiOutput("ui")
    )
    
    server <- function(input, output, session) {
      USER <- reactiveValues(Logged = FALSE)
      observe({
        if (USER$Logged == FALSE) {
          output$ui <- renderUI({
            tagList(
              h2("Hello", align = "center"),
              div(
                id = "login",
                wellPanel(
                  textInput("userName", "Username"),
                  passwordInput("passwd", "Password"),
                  br(), actionButton("Login", "Log in")
                )
              )
            )
          })
        } else if (USER$Logged == TRUE) {
          output$ui <- renderUI({
            tagList(
              tabPanel(""),
              pageWithSidebar(
                headerPanel(
                  ""
                ),
                sidebarPanel(
                  actionButton("logout", "Logout")
                ),
                mainPanel(tableOutput("aaa"))
              )
            )
          })
        }
      })
    
      observeEvent(input$Login, {
        USER$Logged <- TRUE
      })
    
      observeEvent(input$logout, {
        USER$Logged <- FALSE
      })
    }
    
    shinyApp(ui, server)
    

    【讨论】:

    • 感谢@GyD 的回答,并对迟到的回复感到抱歉。我实际上正在寻找一个不需要将uiserver 部分结合起来的答案
    • @EsterSilva 好吧,不做服务器端是不可能的,因为用户的登录/注销可能发生在每个会话中。我也不明白为什么这是个问题。
    猜你喜欢
    • 2020-02-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-23
    • 2020-04-16
    • 1970-01-01
    • 2016-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多