2014-12-26 84 views
0

我想製作一個簡單的閃亮分數查詢系統。也就是說,當有人輸入他的學生證時,他可以得到他的分數。我嘗試了很多,但無法得到結果。以下是我構建的框架。那麼有誰能告訴我我的程序有什麼問題嗎?閃亮做分數查詢系統

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 ,它可以輸出相應的分數。

回答

0

我沒有你的數據,所以不能看到這是否工作,但這個想法應該工作。

的邏輯是: 1.獲得輸入ID,傳遞到server.R 2.在server.R,發現排在你的學生證 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}) 
}) 
}) 

} 
)