基于R Shiny开发学生成绩应用:实现成绩加分及及格/不及格统计
Got it, let's build out this Shiny app to meet your exact requirement: letting users add a custom score to each student's existing grade, then viewing a summary of Pass/Fail outcomes. Let's refine your code step by step, fixing small issues and adding the missing functionality.
Your original data setup had some unnecessary type conversion work—let's create the initial data frame properly from the start to avoid headaches:
library(shiny) # Base student grade data with clear identifiers student_grades <- data.frame( student_id = 1:4, biology_num = c(49, 64, 74, 84) ) # Initial Pass/Fail calculation (we'll update this dynamically later) student_grades$biology_pass_fail <- ifelse(student_grades$biology_num < 50, "Fail", "Pass")
We need to add three key elements to your basic UI:
- A subject selector (you started this)
- A numeric input for the custom score to add
- Output areas to display updated grades and the Pass/Fail summary
Here's the complete UI:
ui <- fluidPage( titlePanel("Student Grade Adjuster & Pass/Fail Summary"), sidebarLayout( sidebarPanel( selectInput("subject", "Choose Subject", choices = list("Biology" = "biology_num")), numericInput("add_score", "Score to Add to Each Student", value = 0, min = 0, max = 50, step = 1), actionButton("update", "Update Grades & Summary") ), mainPanel( h3("Updated Student Grades"), tableOutput("updated_grades"), h3("Pass/Fail Breakdown"), verbatimTextOutput("pass_fail_summary") ) ) )
This is where the dynamic magic happens:
- We'll react to the user's input score when they click the update button
- Calculate adjusted grades for every student
- Re-evaluate Pass/Fail status based on the new grades
- Generate a clear summary of how many students passed/failed
Here's the server code:
server <- function(input, output) { # Reactive expression to refresh data only when the update button is clicked updated_data <- eventReactive(input$update, { temp_data <- student_grades # Add user-specified score to the selected subject's grades temp_data$adjusted_grade <- temp_data[[input$subject]] + input$add_score # Re-calculate Pass/Fail (using 50 as the threshold) temp_data$adjusted_pass_fail <- ifelse(temp_data$adjusted_grade < 50, "Fail", "Pass") temp_data }) # Render the table of updated student grades output$updated_grades <- renderTable({ updated_data() }) # Render the concise Pass/Fail summary output$pass_fail_summary <- renderPrint({ summary_counts <- table(updated_data()$adjusted_pass_fail) cat("Class Performance Summary:\n") print(summary_counts) }) } # Launch the Shiny app shinyApp(ui = ui, server = server)
- Controlled Updates: Using
eventReactiveensures we only recalculate grades when the user clicks "Update"—no constant re-runs as they tweak input values. - Scalability: Adding more subjects later is easy—just add a new column to
student_gradesand update thechoiceslist inselectInput. - Clarity: We show both individual student changes and a high-level summary, so users can dig into details or get a quick overview.
Test it out: if you add 1 to the biology scores, the first student's grade jumps from 49 to 50, switching from "Fail" to "Pass"—the summary will update instantly to reflect this change!
内容的提问来源于stack exchange,提问作者osbwh

