2

I really need help( I've tried a lot of different cases and searched a lot of forums and tutorials(( The aim is simple: I have some MySQL database and table in it. I want to insert values from shiny input into that table. I need actionButton instead submitButton, because there is another important part of my app with live update. Also, I've tried the part of code away from Shiny, and everything worked. So, the problem is in Shiny commands, but I do not know in what. Here is the part of my code (I've deleted almost everything and left just one selectInput field and actionButton):

server.R

library(shiny)
library(RMySQL)

# function to connect to MySQL database and sending query of INSERT
writingMarks <- function(course,homework,valueToInsert){
     courseDB <- dbConnect(MySQL(), user="root", password="password",
                           host="111.111.111.1", db=course)
     query <- paste("INSERT INTO `",course,"`.`homework",homework,
                    "` values (",rowToInsert,")",sep="")
     dbSendQuery(conn=courseDB, query)
     connections <- dbListConnections(MySQL())
     for(i in connections) {dbDisconnect(i)}
}


shinyServer(
     function(input,output,session){
# here I've put "mathematics", "1", "test" instead my input variables
          output$uploadMarks <- 
               reactive({writingMarks("mathematics","1","test")})
     }
)

ui.R

library(shiny)

shinyUI(fluidPage(
          fluidRow(
               column(2,selectInput("task1", label=NULL,
                                    choices = c(0:5), 
                                    selected = 0))
               ),
          actionButton("uploadMarks","Занести оценки в журнал")
     )
)
  • It seems you need to define `rowToInsert` or use `valueToInsert`. Also `connections <- dbListConnections(MySQL()) for(i in connections) {dbDisconnect(i)}` can be replaced with `lapply( dbListConnections( dbDriver( drv = "MySQL")), dbDisconnect)` to simplyfy it. See: [here](http://stackoverflow.com/a/19460534/2213164) for details. – Stanislav Feb 16 '15 at 23:53

1 Answers1

3

I have not checked the writing to MySql function, just the shiny part. Let me know if it works.
You should only care about the modifications in shinyServer.

library(shiny)

ui.R <- shinyUI(
  fluidPage(
    fluidRow(
      column(2,selectInput("task1", label=NULL,
                           choices = c(0:5), 
                           selected = 0))
    ),
    actionButton("uploadMarks","Занести оценки в журнал")
  )
)





library(shiny)
library(RMySQL)
Sys.setlocale(category = "LC_ALL", locale = "Russian")



server.R<-shinyServer( ##here
  function(input,output,session){
    observe({
      # Take a dependency on input$uploadMarks
      if(input$uploadMarks==0) return()

      #Use isolate() to avoid dependency on input$task1
      isolate({
        current_selection<-input$task1
        writingMarks("mathematics",current_selection,"test")
      })#iso
    }
    )
  }
)


runApp(list(
  ui={ ui.R},
  server =  {
    # function to connect to MySQL database and sending query of INSERT
    writingMarks <- function(course,homework,valueToInsert){
      courseDB <- dbConnect(MySQL(), user="root", password="password",
                            host="111.111.111.1", db=course)
      query <- paste("INSERT INTO `",course,"`.`homework",homework,
                     "` values (",rowToInsert,")",sep="")
      dbSendQuery(conn=courseDB, query)
      connections <- dbListConnections(MySQL())
      for(i in connections) {dbDisconnect(i)}
    }
    server.R
  }
))
Stanislav
  • 2,629
  • 1
  • 29
  • 38