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

Use textInput as part of the query in dbGetquery() in flexdashboard

我想在 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
})

第二列的错误是:

cannot coerce class '"function"' to a 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, "'"))
})

当我点击 'Run Document' 时,列名称显示在右列中。我输入文字后出现错误:

non-numeric argument to binary operator

看起来我需要一个反应值的占位符,所以我简单地添加了 v <- reactiveValues(t = NULL) 并使用 t 作为列表 v 中的元素之一。这里是linkhttp://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
})