将数据从 Shiny 应用上传到 MySQL 数据库

uploading data from Shiny app into MySQL database

我真的需要帮助(我尝试了很多不同的案例并搜索了很多论坛和教程(( 目的很简单:我有一些 MySQL 数据库和 table 在里面。我想将闪亮输入的值插入 table。我需要 actionButton 而不是 submitButton,因为我的应用程序的另一个重要部分是实时更新。 此外,我已经尝试了远离 Shiny 的部分代码,并且一切正常。所以,问题出在闪亮的命令中,但我不知道是什么。 这是我的部分代码(我几乎删除了所有内容,只留下一个 selectInput 字段和 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","Занести оценки в журнал")
     )
)

我还没有检查 MySql 函数的写入,只是闪亮的部分。让我知道它是否有效。
您应该只关心 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
  }
))