在单击actionButton之前显示通知

时间:2018-01-19 15:23:38

标签: r shiny

如果数据库中存在用户名,密码或电子邮件,则以下脚本需要显示错误。如果他们不这样做,它会将它们插入数据库并显示通知。

它有效,但问题是,如果在第一次尝试中用户可以成功插入他的信息,在下一次尝试之前点击regist按钮(它是actionButton), 它会显示通知,因为已单击regist按钮。所以我的问题是:如何在每次点击后重置regist按钮的值,或者在点击regist按钮之前如何避免显示消息并采取行动?

并欣赏!

observe({
    if (USER$registed == TRUE){
        if (!is.null(input$regist)){
            if (input$regist > 0){    
                if(is.null(input$uname) || input$uname == "" || is.null(input$pswd) || input$pswd == "" || is.null(input$email) || input$email == "")
                {
                    showNotification("You Forgot to insert your information!", duration = 3, type = c("error"))
                }else{
                    Username <- isolate(input$uname)
                    Password <- isolate(input$pswd)
                    Email <- isolate(input$email)
                    db <- dbConnect(SQLite(), dbname="db.sqlite")
                    existed <- RSQLite::dbGetQuery(db, sprintf({"SELECT rowid FROM users WHERE username='%s' OR password ='%s' OR email='%s'"}, Username, Password, Email, serialize=F))
                    if (nrow(existed)>=1 ) 
                    {
                        showNotification("This Username, Password or Email already exist in the system. Please try something else!", duration = 4, type = c("error"))
                    }else{
                        dbSendQuery(db, sprintf({"INSERT INTO users (username,password,email) VALUES ('%s','%s','%s')"},Username, Password,Email))
                        MAX_studentID <- dbGetQuery(db,"SELECT MAX(studentID) FROM UserID_Map")
                        New_user_StudentID <- as.integer(MAX_studentID)+1
                        dbSendQuery(db, sprintf({"INSERT INTO UserID_Map (username,studentID) VALUES ('%s','%s')"},Username, New_user_StudentID))
                        showNotification("Successful Registeration. You can Login!", duration = 4, type = c("message"))
                    }
                    RSQLite::dbDisconnect(db)
                }
            }
        }
    }
})

1 个答案:

答案 0 :(得分:1)

也许将其设为observeEvent会解决您的问题?这样,代码仅在有人单击actionButton时触发。例如:

observeEvent(input$regist, {
    if (USER$registed == TRUE){
        if (!is.null(input$regist)){
            if (input$regist > 0){    
                if(is.null(input$uname) || input$uname == "" || is.null(input$pswd) || input$pswd == "" || is.null(input$email) || input$email == "")
                {
                    showNotification("You Forgot to insert your information!", duration = 3, type = c("error"))
                }else{
                    Username <- isolate(input$uname)
                    Password <- isolate(input$pswd)
                    Email <- isolate(input$email)
                    db <- dbConnect(SQLite(), dbname="db.sqlite")
                    existed <- RSQLite::dbGetQuery(db, sprintf({"SELECT rowid FROM users WHERE username='%s' OR password ='%s' OR email='%s'"}, Username, Password, Email, serialize=F))
                    if (nrow(existed)>=1 ) 
                    {
                        showNotification("This Username, Password or Email already exist in the system. Please try something else!", duration = 4, type = c("error"))
                    }else{
                        dbSendQuery(db, sprintf({"INSERT INTO users (username,password,email) VALUES ('%s','%s','%s')"},Username, Password,Email))
                        MAX_studentID <- dbGetQuery(db,"SELECT MAX(studentID) FROM UserID_Map")
                        New_user_StudentID <- as.integer(MAX_studentID)+1
                        dbSendQuery(db, sprintf({"INSERT INTO UserID_Map (username,studentID) VALUES ('%s','%s')"},Username, New_user_StudentID))
                        showNotification("Successful Registeration. You can Login!", duration = 4, type = c("message"))
                    }
                    RSQLite::dbDisconnect(db)
                }
            }
        }
    }
})

希望这有帮助!