从Shiny R中的SQLite检索用户名和密码

时间:2017-12-27 15:47:58

标签: r sqlite shiny

我正在开发一个<?php $postArray = json_decode(file_get_contents('php://input'), true); // now you access the values nearly as before: $source = $postArray['source']; $target = $postArray['target']; $arrayString = $postArray['arrayString']; echo json_encode(array('source'=>$source,'target'=>$target,'arrayString'=>$arrayString)); ?> 应用程序。到目前为止,我可以制作以下脚本,但我打算从shiny检索用户名和密码,我无法弄明白。我的意思是,我需要在SQLite的表格中输入我的用户名和密码,然后检查插入的用户名和密码是否匹配。

任何帮助都会很棒。

欣赏!

server.R

SQLite

ui.R

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

  USER <- reactiveValues(Logged = Logged)

  observe({ 
    if (USER$Logged == FALSE) {
      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
            } 
          }
        } 
      }
    }    
  })
  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 = "Contratulations you got in!",ui2())))
      })
      print(ui)
    }
  })
})

ui1.R

ui = (htmlOutput("page"))

ui2.R

    Logged = FALSE;
    my_username <- "ester"
    my_password <- "silva"
    ui1 <- function(){
          tagList(
            div(id = "login",
                wellPanel(textInput("userName", "Username"),
                          passwordInput("passwd", "Password"),
                          br(),actionButton("Login", "Log in"))),
            tags$style(type="text/css", "#login {font-size:10px;   text-align: left;position:absolute;top: 40%;left: 50%;margin-top: -100px;margin-left: -150px;}")
 )}

1 个答案:

答案 0 :(得分:2)

初始设置

最初,您需要创建数据库 - 文件db.sqlite,其中存储所有表(用户数据)。文件应与ui.Rserver.R位于同一文件夹中 - 如果不是,请更改dbname="relative_path/db.sqlite"参数中的路径。

library(RSQLite)
# creates connection to SQLite db, if not exists - creates one
db <- dbConnect(SQLite(), dbname="db.sqlite")

# create table users where logins and passwords are stored
dbSendQuery(conn = db,
        "CREATE TABLE users
        (username TEXT,
        password TEXT)")

# insert some initial data to work with
dbSendQuery(db, "INSERT INTO users ( username, password)
                 VALUES ( 'ester', 'silva' );")

代码修改

而不是你的行。

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
   } 
 }

这样做,从R切换到SQLite功能。查询结果是rowid用户,可以将其视为uid并进一步传递以获取用户数据。

      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
      }

工作示例

library(shiny)
Logged = FALSE;
my_username <- "ester"
my_password <- "silva"
ui2 <- function(){tagList(tabPanel("Test"))}

ui1 <- function(){
  tagList(
    div(id = "login",
        wellPanel(textInput("userName", "Username"),
                  passwordInput("passwd", "Password"),
                  br(),actionButton("Login", "Log in"))),
    tags$style(type="text/css", "#login {font-size:10px;   text-align: left;position:absolute;top: 40%;left: 50%;margin-top: -100px;margin-left: -150px;}")
  )}

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

  USER <- reactiveValues(Logged = Logged)

  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 = "Contratulations you got in!",ui2())))
      })
      print(ui)
    }
  })
})
ui = (htmlOutput("page"))


shinyApp(ui = ui, server = server)