Using SQL Database Using Brilliant Queries
I want to use Shiny so that the user can create a data frame by accessing a SQL database. I want it to work like this (but open to suggestions):
- User types in ID (its number)
- User clicks an action button
- Reactive request is generated
- The request is sent and the data is fetched and added to the data frame
- User types in the following ID ...
Here's my attempt:
interface
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 Id and the request will print correctly. However, I am getting this error when launching applications:
error in evaluating the argument 'statement' in selecting a method for function 'dbGetQuery': Error:
Then when I enter the plate 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 a subset
Error in <my code>: object of type "closure" is not a subset
What I am trying to multiply the underlying R function without specifying the variable name ... but I feel stupid, I looked at it for a day and couldn't see it. Maybe all my code is a bad way to query SQL brilliantly? Any help is appreciated.
Oh yes, ok ... if I change this:
wq <- reactive({ rbind(wq, query( a() )) })
:
wq <- reactive({ query( a() ) })
Then I get the output. So sorry, I guess my question changes to how to then populate the df with each action button click?
source to share
Store your last object in the list, which you define as:
wq<- reactiveValues() .... isolate()
I am working on something similar with updating a model operator with reactively expressed interaction conditions. I had to use reactiveValues()
and isolate()
to get it to work. Joe Cheng has an example of Gist.
Here is the link. Maybe this will help you. https://gist.github.com/jcheng5/eaedfed5095d37217fca
Best, NF
source to share