rshinyflexdashboardroracle

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


I want to include a text input as part of the query in dbGetquery(). It shows the results in normal R script, but shows an error in 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
})

The error in the second column is:

cannot coerce class '"function"' to a data.frame

I have also tried removing the observeEvent and only having renderTable. Like this:

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, "'"))
})

When I hit 'Run Document', the column names show in the right column. After I put the text input, there is an error:

non-numeric argument to binary operator


Solution

  • It looks like I needed a placeholder for the reactive value, so I simply added v <- reactiveValues(t = NULL) and use t as one of the element in the list v. Here is the link http://shiny.rstudio.com/articles/action-buttons.html. I used Pattern 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
    })