使用来自服务器的 MySQL 数据更新 Shiny R 中的 selectInput

updating selectInput in Shiny R with MySQL data from server

我需要写一些代码,它有点像网络日志。问题是我需要在服务器上连接到我的 SQL 数据库,然后下载一个 table (students) 并在字段 table 中显示这个 table 的列=13=]。我想将 table students 存储到数据框学生中,然后在 selectInput("student", "Имя:", "") 中的 ui.R 中显示此 table 中的列。这是我的代码:

server.R

library(shiny)
library(RMySQL)

courseDBChoise <- function(course){
courseDB <- dbConnect(MySQL(), user="root", password="password",
                       host="111.111.111.1", db=course)
dbSendQuery(conn=courseDB, "SET NAMES utf8")
students <<- dbReadTable(conn = courseDB, "students")
return(students)}


shinyServer(
     function(input,output,session){
          test <- reactive({courseDBChoise(input$course)})
          observe({
               updateTextInput(session, "student", 
                               value = test)
          })

     }
)


ui.R

library(shiny)

shinyUI(fluidPage(
     titlePanel("Журнал преподавателя"),
     sidebarPanel(
          h4("Пожалуйста, выберете курс, фамилию ученика и номер работы:"),
          br(),br(),
          selectInput("student", "Имя:", ""),
          selectInput("course", "Курс:",
                      list("Математика"="mathematics",
                           "Физика"="physics",
                           "Химия"="chemistry")),
          selectInput("homework","№ Работы",as.character(1:30)),
          br(), br(), br(), br(), br(), br(), br(), br(), br(), br(), br(),
          img(src="head.png",height=75,width=75,align="center"),
          "ФФФХИ, МГУ, Москва"
          ),
     mainPanel(wellPanel(
          h4("Введите оценки, полученные за работу"),
          br(),
          fluidRow(
               column(2,strong("1"),align="center"),
               column(2,strong("2"),align="center"),
               column(2,strong("3"),align="center"),
               column(2,strong("4"),align="center"),
               column(2,strong("5"),align="center"),
               column(2,strong("6"),align="center")
               ),
          fluidRow(
               column(2,textInput("N1",label="",value=NA)),
               column(2,textInput("N2",label="",value=NA)),
               column(2,textInput("N3",label="",value=NA)),
               column(2,textInput("N4",label="",value=NA)),
               column(2,textInput("N5",label="",value=NA)),
               column(2,textInput("N6",label="",value=NA))
               ),
          br(),
          fluidRow(
               column(2,strong("7"),align="center"),
               column(2,strong("8"),align="center"),
               column(2,strong("9"),align="center"),
               column(2,strong("10"),align="center"),
               column(2,strong("11"),align="center"),
               column(2,strong("12"),align="center")
               ),
          fluidRow(
               column(2,textInput("N7",label="",value=NA)),
               column(2,textInput("N8",label="",value=NA)),
               column(2,textInput("N9",label="",value=NA)),
               column(2,textInput("N10",label="",value=NA)),
               column(2,textInput("N11",label="",value=NA)),
               column(2,textInput("N12",label="",value=NA))
               ),
          br(),
          submitButton("Занести оценки в журнал"), 
          align="center"
          ))
     )
)

这是我修改后的版本。我希望它有所帮助,现在还不算太晚。 我已经用 ##here 标记了代码中的所有更改。

server.R

#Change the language to Russian (You probably do not need this)
Sys.setlocale(category = "LC_ALL", locale = "Russian") ##here

library(shiny)
library(RMySQL)

courseDBChoise <- function(course){
  courseDB <- dbConnect(MySQL(), user="root", password="password",
                        host="111.111.111.1", db=course)
  dbSendQuery(conn=courseDB, "SET NAMES utf8")
  students <- dbReadTable(conn = courseDB, "students")

  #it is important to make sure you return just one column
  return(students[,1])##here
}


shinyServer(
  function(input,output,session){

    test <- reactive({
      courseDBChoise(input$course)
    })

    #update the selectable
    #Here we are sending to ui.R its content
    output$student <- renderUI({ ##here
      selectInput("student", "Имя:", as.list(test()))##here we call the function test
    })
  }
)

ui.R

library(shiny)

shinyUI(fluidPage(
  titlePanel("Журнал преподавателя"),
  sidebarPanel(
    h4("Пожалуйста, выберете курс, фамилию ученика и номер работы:"),
    br(),br(),

    #Here is where our server will put the selectable
    uiOutput("student"),##here
    selectInput("course", "Курс:",
                list("Математика"="mathematics",
                     "Физика"="physics",
                     "Химия"="chemistry")),
    selectInput("homework","№ Работы",as.character(1:30)),
    br(), br(), br(), br(), br(), br(), br(), br(), br(), br(), br(),
    img(src="head.png",height=75,width=75,align="center"),
    "ФФФХИ, МГУ, Москва"
  ),
  mainPanel(wellPanel(
    h4("Введите оценки, полученные за работу"),
    br(),
    fluidRow(
      column(2,strong("1"),align="center"),
      column(2,strong("2"),align="center"),
      column(2,strong("3"),align="center"),
      column(2,strong("4"),align="center"),
      column(2,strong("5"),align="center"),
      column(2,strong("6"),align="center")
    ),
    fluidRow(
      column(2,textInput("N1",label="",value=NA)),
      column(2,textInput("N2",label="",value=NA)),
      column(2,textInput("N3",label="",value=NA)),
      column(2,textInput("N4",label="",value=NA)),
      column(2,textInput("N5",label="",value=NA)),
      column(2,textInput("N6",label="",value=NA))
    ),
    br(),
    fluidRow(
      column(2,strong("7"),align="center"),
      column(2,strong("8"),align="center"),
      column(2,strong("9"),align="center"),
      column(2,strong("10"),align="center"),
      column(2,strong("11"),align="center"),
      column(2,strong("12"),align="center")
    ),
    fluidRow(
      column(2,textInput("N7",label="",value=NA)),
      column(2,textInput("N8",label="",value=NA)),
      column(2,textInput("N9",label="",value=NA)),
      column(2,textInput("N10",label="",value=NA)),
      column(2,textInput("N11",label="",value=NA)),
      column(2,textInput("N12",label="",value=NA))
    ),
    br(),
    submitButton("Занести оценки в журнал"), 
    align="center"
  ))
)
)

MySQL 连接问题排查

如果您在使用命令 dbConnect(MySQL(), user="<username>", password="<password>", host="<server.example.com>", db=<databasename>) 时遇到问题,那么我强烈建议您打开一个终端并测试是否可以使用这些凭据连接到您的服务器:mysql -u <username> -p -h <server.example.com> -D <databasename>

更多MySQL故障排除请参考:link1, link2