我正在尝试使用 SQLite 数据库开发 R Shiny 应用程序。但我在从数据库获取任何数据时遇到此错误。错误信息是:
Loading required package: shiny
Attaching package: ‘dplyr’
The following objects are masked from ‘package:stats’:
filter, lag
The following objects are masked from ‘package:base’:
intersect, setdiff, setequal, union**
在应用程序内,它显示:
[object Object]
有人可以帮助我吗?您对开发 SQLite 数据库有什么建议吗?
代码如下:
# Libraries
library(stats)
library(shiny)
library(RSQLite)
library(dtplyr)
library(dplyr)
library(DBI)
# Globar Variables
db <- dbConnect (SQLite(),
"/Users/sudiptobosu/200922/20200714PPCP.sqlite")
# Global Function
names_ppcp = dbGetQuery(db, "SELECT ALL PPCP FROM mydata")
#server
shinyServer(function (input, output, session) {
dplyr::filter
stats::filter
output$ppcpOut <- reactive({
input$user1
})
output$propertyOut <- reactive({
input$user2
})
ppcpquery <- reactive({
input$user1
})
queryy <- reactive({
gsub("<ppc>", ppcpquery(), "SELECT Solubility FROM mydata WHERE PPCP = '<ppc>'")
})
output$valueout <- reactive({
dbGetQuery(db, queryy())
})
#propertyquery <- eventReactive ({input$user2})
session$onSessionEnded (function() {
dbDisconnect(db)
})
})
#ui
ui <- fluidPage (
titlePanel("bo-DEREC CE"),
sidebarLayout(
sidebarPanel(
selectInput("user1","Select the PPCP",choices = names_ppcp),
selectInput("user2","Select the property",choices = c("Solubility","Volatility","Adsorbability","Degradability"))
),
mainPanel(
textOutput("ppcpOut"),
textOutput("propertyOut"),
textOutput("valueout")
)
)
)
数据框不应在用户界面上使用
textOutput
渲染,而应使用 tableOutput
渲染。然后将反应性呼叫调整为renderTable
。通常 input$
对象应该包裹在 reactive
中。另外,请考虑正确参数化,例如使用 DBI:sqlInterpolate
:
服务器调整
...
# INPUTS
ppcpquery <- reactive({
input$user1
})
propertyquery <- reactive({
input$user2
})
# OUTPUTS
output$ppcpOut <- renderText({
ppcpquery()
})
output$propertyOut <- renderText({
propertyquery()
})
output$valueOut <- renderTable({
# PREPARED STATEMENT WITH PLACEHOLDER
sql <- "SELECT Solubility FROM mydata WHERE PPCP = ?ppc")
# BIND PARAMETER
query <- DBI::sqlInterpolate(db, sql, ppc = ppcpquery())
# RUN QUERY AND RETURN RESULTS
dbGetQuery(db, query)
})
ui调整
...
mainPanel(
textOutput("ppcpOut"), # ALIGNS TO renderText
textOutput("propertyOut"), # ALIGNS TO renderText
tableOutput("valueOut") # ALIGNS TO renderTable
)
现在,如果
dbGetQuery
返回单行一列的数据框,那么您可以在 renderText
中使用 server
,在 textOutput
中使用 ui
。但从列中提取值:
dbGetQuery(db, query)$Solubility[[1]]