【发布时间】:2016-06-06 00:03:08
【问题描述】:
我遇到了 R shiny 和 sqlite 的问题。我的应用应该对用户进行身份验证并加载他/她的偏好。
这是我的代码:
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)
我的问题是当我想为查询放置 TextInput 的内容时。我尝试了几种方法
在当前版本中,renderPrint(input$username) 会返回一些似乎是函数但似乎没有用的东西。
我也尝试了另一种方式,只使用
userName=paste(input$userName)
这将返回 textField 的内容,但是当我将它集成到查询中时,它会放置
[1] "SELECT password from USERS where name = \" test \""
然后我得到了错误
Warning: Error in matrix: length of 'dimnames' [2] not equal to array extent
我的目标是进行这样的查询
"Select password FROM USERS where name = "username"
用username代表TextInput的内容。
编辑
我知道使用这个版本的查询,它提出了一个语法正确的查询
qry = paste0('SELECT password from USERS where name = \'',userName,'\'')
res= dbGetQuery(db,qry )
但我现在面临这个问题:
Warning: Error in matrix: length of 'dimnames' [2] not equal to array extent
当我运行该方法时
connect(db,qry)
我认为问题出在我获取 TextInput 内容的方式上:我使用
function(input, output,session) {
observeEvent(input$connectButton, {
userName= paste0(input$username)
userPwd = paste0(input$password)
connect(user = userName,pwd = userPwd)
})
您对此有何看法?
【问题讨论】:
-
尝试
paste0()而不是paste()。后者默认在术语之间插入空格。
标签: r sqlite shiny textinput shinydashboard