【问题标题】:Use textInput as part of the query in dbGetquery() in flexdashboard在 flexdashboard 的 dbGetquery() 中使用 textInput 作为查询的一部分
【发布时间】:2019-07-03 12:55:17
【问题描述】:

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

第二列的错误是:

无法将类 '"function"' 强制转换为 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, "'"))
})

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

二元运算符的非数字参数

【问题讨论】:

  • renderTable({t}) 中,t 是一个反应函数。有几种方法可以从函数中获取 data.frame,但最简单的是renderTable({ t() })
  • 我不是 100% 确定 observeEvent 会如何影响这一点。您实际上可能需要renderTable({ session() })
  • 您能详细说明一下吗?我试过renderTable({ t(input$Col) }),错误消失了,但是表格只有一列叫做V1,单元格是输入...
  • 我试过renderTable({ session() }),结果和上面一样……
  • 嗯。 t() 是一个称为转置的基础 r 函数。也许尝试将表命名为不是基本 r 函数的东西?

标签: r shiny flexdashboard roracle


【解决方案1】:

看起来我需要一个占位符来表示反应值,所以我只是添加了v &lt;- 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
})

【讨论】:

    猜你喜欢
    • 2021-04-26
    • 2019-11-05
    • 2010-12-11
    • 2020-09-12
    • 2013-12-03
    • 2018-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多