-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.R
178 lines (156 loc) · 4.63 KB
/
app.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# Load necessary libraries
library(shiny)
library(shinymaterial)
library(shinydashboard)
library(shinyjs)
library(shinyWidgets) # Added shinyWidgets library
library(dplyr)
# Define custom CSS
css <- "
body {
background-color: #f7f7f7;
font-family: 'Helvetica Neue', sans-serif;
}
.sidebar {
background-color: #333;
color: white;
}
.main-header {
background-color: #333;
color: white;
}
.sidebar-menu .treeview-menu > li.active > a {
background-color: #555;
}
.box {
border-radius: 5px;
box-shadow: 0 0 5px rgba(0, 0, 0, 0.2);
}
.btn-primary {
background-color: #007bff;
border-color: #007bff;
}
.btn-primary:hover {
background-color: #0056b3;
border-color: #0056b3;
}
#calculate {
margin-top: 20px;
}
#download_pdf {
margin-top: 20px;
}
#best_wishes {
font-style: italic;
color: #888;
}
#cgpa_result {
font-size: 24px;
font-weight: bold;
color: #007bff;
}
"
# Define the UI
ui <- dashboardPage(
dashboardHeader(title = "CGPA Calculator", titleWidth = 250),
dashboardSidebar(
width = 200,
sidebarMenu(
menuItem("Calculator", tabName = "calculator", icon = icon("calculator")),
menuItem("History", tabName = "history", icon = icon("history"))
),
tags$style(HTML(css))
),
dashboardBody(
tabItems(
tabItem(
tabName = "calculator",
fluidPage(
sidebarLayout(
sidebarPanel(
width = 500,
numericInput("num_courses", "Number of Courses:", value = 3, min = 1),
tags$hr(),
uiOutput("course_info"),
actionButton("calculate", "Calculate CGPA", class = "btn-primary"),
downloadButton("download_pdf", "Download as PDF", class = "btn-primary")
),
mainPanel()
)
)
),
tabItem(
tabName = "history",
dataTableOutput("history_table")
)
)
)
)
# Define the server
server <- function(input, output, session) {
history_data <- reactiveVal(data.frame(Time = character(), CGPA = numeric()))
output$course_info <- renderUI({
num_courses <- input$num_courses
course_info <- lapply(1:num_courses, function(i) {
fluidRow(
column(4,
selectInput(inputId = paste0("course_grade_", i),
label = paste("Course", i, "Grade:"),
choices = c("A+"= 4.0, "A" = 4.0, "A-" = 3.7, "B+" = 3.3, "B" = 3.0, "B-" = 2.7, "C+" = 2.3, "C" = 2.0, "C-" = 1.7, "D" = 1.0, "F" = 0),
selected = "A")
),
column(4,
numericInput(inputId = paste0("course_credits_", i),
label = paste("Course", i, "Credits:"),
value = 3, max = 4.5, min = 1, step = .5)
)
)
})
do.call(tagList, course_info)
})
observeEvent(input$calculate, {
grades <- sapply(1:input$num_courses, function(i) as.numeric(input[[paste0("course_grade_", i)]]))
credits <- sapply(1:input$num_courses, function(i) as.numeric(input[[paste0("course_credits_", i)]]))
# Calculate GPA based on the selected grade points and credits
gpacrs <- grades * credits
total_credits <- sum(credits)
total_gpacr <- sum(gpacrs)
cgpa <- total_gpacr / total_credits
# Add results to the history_data
timestamp <- format(Sys.time(), "%Y-%m-%d %H:%M:%S")
history_data_data <- history_data()
history_data_data <- rbind(history_data_data, data.frame(Time = timestamp, CGPA = cgpa))
history_data(history_data_data)
# Show CGPA in a modal dialog
showModal(modalDialog(
title = div(style = "text-align: center; font-weight: bold; background-color: #007bff; color: white;",
"CGPA Result"),
div(style = "text-align: center;", h4(paste("Your CGPA is:", round(cgpa, 2)))),
footer = NULL,
easyClose = TRUE,
div(style = "text-align: center; color: #007bff; font-weight: bold;", h1("Best Wishes"))
))
})
output$download_pdf <- downloadHandler(
filename = function() {
paste("cgpa_report.pdf")
},
content = function(file) {
pdf(file, width = 8, height = 6)
cat("CGPA Report\n\n")
cat("Your CGPA is:", round(cgpa, 2), "\n")
cat("Course Details:\n")
for (i in 1:input$num_courses) {
cat("Course", i, "Grade:", input[[paste0("course_grade_", i)]], "\n")
cat("Course", i, "Credits:", input[[paste0("course_credits_", i)]], "\n\n")
}
dev.off()
file.copy("cgpa_report.pdf", file)
}
)
output$history_table <- renderDataTable({
history_data()
})
}
# Run the Shiny app
shinyApp(ui, server, options = list(sessionTimeout = 60*60)) # Set to 1 hour