mardi 23 février 2016

R Shiny put content of a TextInput in sql queries

I'm facing a problem with R shiny and sqlite. My app is supposed to authenticate the user and load his/her preferences.

Here is my code :

server.R

library(shiny)
library(shinyBS)
library(dplyr)
library(lubridate)

library(DBI)
library(RSQLite)

############################
#    Database functions    #
###########################

# Connect the user to the dtabase app
connect <- function(userName,pwd){
    #Put a generic path here
  db <- dbConnect(SQLite(), dbname = "my_path/database.db")
  #Query to get the correct passwd
  qry = paste('SELECT password from USERS where name = "',userName,'"')
  res= dbGetQuery(db,qry )

  ifelse(res==pwd,"connected","unable to connect to the database")
  dbDisconnect(db)

}

function(input, output,session) {

  observeEvent(input$connectButton, {
   userName= renderPrint(input$username)
   print(userName)
   userPwd = paste(input$password)
       connect(user = userName,pwd = userPwd)

  })

ui.R

shinyUI(fluidPage(
titlePanel("Authentification"),
textInput('username', label="User name"),
textInput('password', label= "password"),
actionButton("connectButton", label='Connect'),
actionButton("subscribeButton",label='Subscribe')   
)
)

app.R

library(shiny)
library(shinyBS)
####### UI
ui <- source("ui.R")
####### Server
varserver <- source("server.R")
####### APP
shinyApp(ui = ui, server = varserver)

My problem is when I want to put the content of the TextInput for the queries. I've tried several methods

With the current version, renderPrint(input$username) returns me something what seems to be a function but it doesn't seem to be useful.

I also tried an other way using only

userName=paste(input$userName)

This returns me the content of the textField but when i integrate it to teh query it puts

[1] "SELECT password from USERS where name = \" test \""

and then i got the error

Warning: Error in matrix: length of 'dimnames' [2] not equal to array extent

My objective is to have a query like this

"Select password FROM USERS where name = "username"

with username representing the content of the TextInput

Could you guys help me to deal with this problem ? Thanks

Aucun commentaire:

Enregistrer un commentaire