【问题标题】:object Object error in R Shiny SQLite AppR Shiny SQLite App中的对象对象错误
【发布时间】:2021-01-15 10:45:43
【问题描述】:

我正在尝试使用 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")
    )
  )
)

【问题讨论】:

    标签: sql r sqlite shiny-server shinyapps


    【解决方案1】:

    数据帧不应在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 返回一行一列的数据框,那么您可以在server 中使用renderText,在ui 中使用textOutput。但是从列中提取值:

    dbGetQuery(db, query)$Solubility[[1]]
    

    【讨论】:

    • 亲爱的 Parfait,非常感谢您的解决方案。真的行!!!但是如果我想在'sql'中使用两个变量怎么办。我该怎么办?我以与您对 ?ppc 相同的方式进行了尝试,我使用了 ?prop 而不是 Solubility,但是当 sql 中有两个变量时,只有一个有效。你有什么建议吗?
    • 你不能参数化标识符,如表和列名,只有文字值。使用paste 或其他字符串插值作为标识符。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-05
    • 1970-01-01
    • 2021-05-18
    • 1970-01-01
    相关资源
    最近更新 更多