How to insert reactive input values from a shiny app into a MySQL database?
Asked Answered
D

2

6

I created an online experiment with the shiny package for R. Let's say I have 3 reactive values called "toss", "decision" and "rating".
Additionally, I launched a MySQL database on Amazon web service RDS. The version is MySQL 5.6.22.
I successfully managed to to send non-reactive values- like the timestamp- to MySQL database. So I assume the problem is where to locate the code talking to MySQL within the Server.R code. For non-reactive values it works perfectly when the code is outside (before) the reactive server-function. But with reactive values I suppose it should be somewhere within.

I tried this code:

Server.R  
   library(shiny)
   library(RMySQL)
   library(DBI)
    con <- dbConnect(MySQL(), dbname="db", username="myname", password="mypassword", host="myhost.com", port=xxxx)
   function(input, output, session){
       sql <- reactive({
                paste("insert into scenario1 (toss, dec, rat, timestamp) 
                     values (",input$toss,",",input$decision,",",input$rating,"now())")
       })
       result<-reactive({dbSendQuery(con, sql())})
   }

This way, I do not get an error message. So maybe the error is within the insert into-code.

Additionally, I'm not sure whether the packages that I used are ideal for this purpose. I tried out a lot of things. Whenever I add a reactive value by leaving it out of the SQL-quote it stops working. I'm starting to think that RMySQL is missing that feature. There is nothing about insert into in the manual.

Is anyone able to detect the mistake I made?

Diverse answered 13/5, 2015 at 11:29 Comment(2)
When do you want that sql run? I think you'll need to wrap your dbSendQuery in an isolate().Tele
@Tele Thanks for your suggestion. Would I simply add isolate(sql()) to my code? By doing this it still doesn't work. So I assume it is not that simple. I want that sql to run at the end of the shiny session.Diverse
D
1

Finally, I could make the query run with this code:

writingMarks <- function(input){ 
    con <- dbConnect(MySQL(), dbname="db", username="myname", password="mypassword", 
           host="myhost.com", port=xxxx)   
    result <- sprintf<-("insert into scenario1 (toss, dec, timestamp) values (%s,%s,%s)",
                input$toss, input$decision, "now()")
    dbSendQuery(con, result)
}

I could not make the paste query run. With sprintf there is less confusion with the commas.

And, yes I indeed had to add an isolate(). I inserted it into an observe(). So it looks like this:

observe({
    if (input$condition==1){
      isolate({
        writingMarks(input)
      })
    }
    return()
  })
Diverse answered 5/11, 2015 at 15:12 Comment(0)
L
0

You have a problem in:

paste("insert into scenario1 (toss, dec, rat, timestamp)
values (",input$toss,",",input$decision,",",input$rating,"now())")

The issue is: No , before now():

paste("insert into scenario1 (toss, dec, rat, timestamp)
values (",input$toss,",",input$decision,",",input$rating,",now())")

That should make the query run. Look into prepared statements, that will prevent this kind of (very common everybody makes them) concatenation mistakes.

Lakia answered 19/7, 2015 at 19:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.