在flexdashboard的dbGetquery()中将textInput用作查询的一部分

时间:2019-07-03 12:55:17

标签: r shiny flexdashboard roracle

我想在dbGetquery()中将文本输入作为查询的一部分。它以普通的R脚本显示结果,但在renderTable()中显示错误。

library(flexdashboard)
suppressWarnings(library(ROracle, quietly = TRUE))
library(shiny)

Column {data-width=350}
-----------------------------------------------------------------------
textInput(inputId = 'Col', label = 'COL', value = "")
actionButton('submit', 'Submit', icon = icon('refresh'))


Column {data-width=650}
-----------------------------------------------------------------------
session <- observeEvent(input$submit, {
  etf_con<- dbConnect(drv, username = load.schema.username, password = load.schema.password, dbname = load.schema.database)
  t <- dbGetQuery(etf_con, paste0("select * from table_name where col = '", input$Col, "'"))
})

renderTable({
  t
})

第二栏中的错误是:

  

无法将类“功能”强制转换为data.frame

我还尝试过删除observeEvent并仅拥有renderTable。像这样:

renderTable({
  etf_con<- dbConnect(drv, username = load.schema.username, password = load.schema.password, dbname = load.schema.database)
  dbGetQuery(etf_con, paste0("select * from table_name where col = '", input$Col, "'"))
})

当我点击“运行文档”时,列名称显示在右列中。输入文本后,出现错误:

  

二进制运算符的非数字参数

1 个答案:

答案 0 :(得分:0)

似乎我需要一个占位符作为反应值,因此我只添加了v <- reactiveValues(t = NULL)并将t用作列表v中的元素之一。这是链接http://shiny.rstudio.com/articles/action-buttons.html。我使用了模式3。

library(flexdashboard)
suppressWarnings(library(ROracle, quietly = TRUE))
library(shiny)

Column {data-width=350}
-----------------------------------------------------------------------
textInput(inputId = 'Col', label = 'COL', value = "")
actionButton('submit', 'Submit', icon = icon('refresh'))


Column {data-width=650}
-----------------------------------------------------------------------
v <- reactiveValues(t = NULL)

observeEvent(input$submit, {
  etf_con<- dbConnect(drv, username = load.schema.username, password = load.schema.password, dbname = load.schema.database)
  v$t <- dbGetQuery(etf_con, paste0("select * from table_name where col = '", input$Col, "'"))
})

renderTable({
  v$t
})