r - 在 flexdashboard 的 dbGetquery() 中使用 textInput 作为查询的一部分
问题描述
我想在 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, "'"))
})
当我点击“运行文档”时,列名显示在右列中。我输入文本后,出现错误:
二元运算符的非数字参数
解决方案
看起来我需要一个占位符来表示反应值,所以我只是添加v <- reactiveValues(t = NULL)
并t
用作 list 中的元素之一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
})
推荐阅读
- python - 让 Django 接受带有无限参数的 url
- shell - 在 shell 脚本文件中的工件版本中出现错误,因为没有这样的目录或文件
- c# - c# 中调用方法的 switch 语句的替代方法
- amazon-dynamodb - 关于基于日期时间的扫描记录
- c# - 如何触发并让滑动菜单返回?
- python-3.x - tkinter 冻结 - 如何使用线程/.after()
- r - 使用 R 脚本创建高度重复的 Rmarkdown 脚本
- driver - 如何在 wdf 驱动程序中修复 PAGE_FAULT_IN_NONPAGED_AREA
- python - 如何重新投影 shapefile?
- python - 从外部预设启动目录加载 Jupyter notebook