3

I need to write some code, it is something like an internet journal. The problem is that I need to connect to my SQL database on a server, then download a table (students) and display the column of this table in the field selectInput. I want to store the table students to dataframe students and then display the column from this table in ui.R in selectInput("student", "Имя:", ""). Here is my code:

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"
          ))
     )
)
Stanislav
  • 2,629
  • 1
  • 29
  • 38

1 Answers1

2

Here is my modified version. I hope it helps and it is not too late. I have marked with ##here all the changes in the code.

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 connection problems troubleshooting

If you have trouble with the command dbConnect(MySQL(), user="<username>", password="<password>", host="<server.example.com>", db=<databasename>), then I highly suggest opening a terminal and testing if it is possible to connect to your server using those credentials: mysql -u <username> -p -h <server.example.com> -D <databasename>

For more MySQL troubleshooting please refer to : link1, link2

Community
  • 1
  • 1
Stanislav
  • 2,629
  • 1
  • 29
  • 38