I want to use Shiny to let a user build a data frame by accessing an SQL data base. I want it to work in the following way (but open to suggestions):
- User types in ID (its a number)
- User presses action button
- Reactive query is generated
- Query is sent and data is retrieved, and added to data frame
- User types in next ID...
Here is my attempt:
UI
library(markdown)
shinyUI(fluidPage(
titlePanel("Generic grapher"),
sidebarLayout(
sidebarPanel(
numericInput("wafer", label = h3("Select wafer ID:"), value = NULL),
actionButton("do", "An action button")
),
mainPanel(
verbatimTextOutput("value"), verbatimTextOutput("que"), dataTableOutput(outputId="pos")
)
)))
Server
library(RMySQL)
library(DBI)
library(sqldf)
con = dbConnect(RMySQL::MySQL(), dbname="Test_DB", username="pete", password="xx", host="xx", port=3306)
query <- function(...) dbGetQuery(con, ...)
wq = data.frame()
df = data.frame()
shinyServer(function(input, output){
d <- eventReactive(input$do, { input$wafer })
output$value <- renderPrint({ d() })
a <- reactive({ paste("Select id from wafer where wafer_id=",d(), sep="") })
output$que <- renderPrint({ a() })
wq <- reactive({ rbind(wq, query( a() )) })
output$pos <- renderDataTable({ wq() })
})
When I run, I can see that both the Id and query will print correctly. However I get this error when app firsts runs:
error in evaluating the argument 'statement' in selecting a method for function 'dbGetQuery': Error:
Then when I type in a wafer id, I get:
Error in xi[[j]] : object of type 'closure' is not subsettable
I know from these posts:
R shiny ERROR: object of type 'closure' is not subsettable
Error in <my code> : object of type 'closure' is not subsettable
That i am probabaly trying to subset an R base function without defining the variable name...but i feel stupid, Ive been staring at it for a day and cant see it. Maybe my whole code is a bad way to query SQL by shiny? Any help appreciated.
Ah dam, ok..if i change this:
wq <- reactive({ rbind(wq, query( a() )) })
to this:
wq <- reactive({ query( a() ) })
Then I get an output. So im sorry, I guess my question changes to how to then populate a df with each additional click of the action button?