闪亮的分数查询系统

时间:2014-12-26 13:35:11

标签: r shiny shiny-server

我想制作一个简单的乐谱查询系统。也就是说,当有人输入他的学生ID时,他可以得到他的分数。我尝试了很多,但无法得到结果。下面是我构建的框架。那么有谁能告诉我我的计划有什么问题?

ui.R

library(shiny)
shinyUI(
pageWithSidebar(
headerPanel("Midterm score"),

sidebarPanel(
  numericInput("studentid","Student ID:",17220131181990),
  submitButton("Submit")
  ),

mainPanel(
  h3("You score"),
  h4("You student id:"),
  verbatimTextOutput("inputValue"),
  h4("You midterm score:"),
  verbatimTextOutput("score")
  )
)

server.R

library(shiny)
data <- read.csv("C:/Users/hmw20_000/Desktop/score.csv")
shinyServer(
function(input,output){
output$inputValue <- renderPrint({input$studentid})
output$score <- renderPrint({data$score})
}
)

得分csv文件有两列:一列是studentid,另一列是score.So当输入studentid时,它可以输出相应的分数。

1 个答案:

答案 0 :(得分:0)

我没有您的数据,所以无法查看这是否有效,但这个想法应该有用。

逻辑是: 1.获取输入ID,传递给server.R 2.在server.R中,找到学生ID数据中的行 3.从该行获得分数 4.将输出文本设置为等于分数

ui.R

library(shiny)
shinyUI(
pageWithSidebar(
headerPanel("Midterm score"),

sidebarPanel(
  # Input the id, and set up a button to submit the value
  numericInput("studentid","Student ID:",17220131181990),
  actionButton("submitButton","Go!")
  ),

mainPanel(
  h3("Your score:"),
  verbatimTextOutput("outputscore")
  )
)

server.R:

library(shiny)
data <- read.csv("C:/Users/hmw20_000/Desktop/score.csv")
shinyServer(
function(input,output){

observe({
# Check if the button is clicked
if(input$submitButton==0) return(NULL)
isolate({
# Get the ID from the input
studentID<-input$studentid
# Get the row in your data which corresponds to the student
n<-which(data$studentid==studentID)
# If it doesn't exist,(or more than one exists), throw an error:
if(length(n)!=1){score<-"Error!"}
# Get the score from that row
if(length(n)==1){score<-score<-data$score[n]}    
# Write the score to the output
output$outputScore<-renderText({score})
})
})

}
)