-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.R
271 lines (240 loc) · 10.1 KB
/
ui.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# Developed by:
# https://github.com/rebberchicken
# DO NOT REPOST OR USE FOR YOUR ACADEMICS.
# This is the UI code for the CMSC 150 final project.
# You can run the application by clicking the 'Run App' button.
library(readr)
library(shiny)
library(shinyjs)
library(shinyMatrix)
library(shinythemes)
source("QuadraticSpline.R")
source("PolynomialReg.R")
# source("SimplexMethod.R")
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
tags$head(
tags$link(
rel = "stylesheet",
href = "https://fonts.googleapis.com/css2?family=Google+Sans:wght@400;700&display=swap"
),
tags$style(
HTML(
"
body { font-family: 'Google Sans', sans-serif; }
.navbar { background-color: #3498db; color: white; }
.navbar-default .navbar-nav > .active > a,
.navbar-default .navbar-nav > .active > a:hover,
.navbar-default .navbar-nav > li > a:hover,
.navbar-default .navbar-nav > li > a:focus { background-color: #2980b9; color: white; }
.navbar-default .navbar-brand { color: white; }
.navbar-default .navbar-brand:hover { color: white; }
.tab-content { background-color: #ecf0f1; }
h1 { font-size: 36px; font-weight: bold; color: #3498db; margin-bottom: 20px; }
h2 { font-size: 28px; font-weight: bold; color: #3498db; margin-bottom: 15px; }
h4 { font-size: 20px; font-weight: bold; color: #3498db; margin-bottom: 10px; }
p { font-size: 16px; color: #555; }
.btn-primary { background-color: #3498db; color: white; border: none; }
.btn-primary:hover { background-color: #2980b9; color: white; }
"
)
) # CSS Styles for the UI
),
shinyjs::useShinyjs(), # Used for some styles
h1("CMSC 150 Project", align = "center"),
navbarPage(
"",
tabPanel(
"QSI Calculator",
wellPanel(
h2("Quadratic Spline Interpolation Calculator", align = "center"),
),
sidebarPanel(
h2("Parameters"),
numericInput("Value", "Enter Value to be Evaluated", 0),
fileInput("file", "Upload CSV File"),
p("Note: CSV file must have two columns for x and y values. Otherwise, it will print as nothing or NULL values."),
actionButton("Generate", label = "Generate", class = "btn-primary")
), # sidebar panel
mainPanel(
h4("Function Per Interval"),
p("Generated Functions Per Interval"),
verbatimTextOutput("qsi.fxns"),
h4("Estimated Value"),
p("Estimated Value of Target"),
verbatimTextOutput("est"),
) # main panel
), # tab panel qsi
tabPanel(
"PR Calculator",
wellPanel(
h2("Polynomial Regression Calculator", align = "center"),
),
sidebarPanel(
h2("Parameters"),
numericInput("polyDegree", "Polynomial Degree", 0),
numericInput("polyXVal", "Enter X Value", 0),
fileInput("polyFile", "Upload CSV File"),
p("Note: CSV file must have two columns for x and y values. Otherwise, it will print as nothing or NULL values."),
actionButton("RunPolyReg", label = "Generate", class = "btn-primary")
),
mainPanel(
h4("Polynomial Function"),
p("Generated Polynomial Function"),
verbatimTextOutput("polyFunction"),
h4("Estimated Value"),
p("Estimated Value of Target"),
verbatimTextOutput("polyEstimate"),
)
), #tab panel pr
tabPanel(
"Simplex Method",
wellPanel(
h2("Simplex For Diet Problem", align = "center"),
),
sidebarPanel(
h2("Parameters"),
# Add UI elements for Simplex Method parameters
selectInput("foodItems", "Select Food Items", choices = NULL, multiple = TRUE),
actionButton("checkAll", "Check All"),
actionButton("resetAll", "Reset All"),
actionButton("solveSimplex", label = "Solve Simplex", class = "btn-primary")
),
mainPanel(
# Add outputs for the Simplex Method
h4("Optimal Diet Plan"),
verbatimTextOutput("simplexIterations"),
h4("Simplex Iterations"),
tableOutput("optimalDiet")
)
)
)
)
# Server function for all actions
server <- function(input, output, session) {
# Create reactive values to store matrix data, set as NULL to avoid bugs
matrixDataQSI <- reactiveVal(NULL)
matrixDataPoly <- reactiveVal(NULL)
# ================== QUADRATIC SPLINE ==================
# QSI Reactive expression when "Generate" button is clicked
quadraticSInterpolation <- eventReactive(input$Generate, {
req(matrixDataQSI()) # checks if matrixDataQSI has a value
x <- matrixDataQSI()[, 1] # first column
y <- matrixDataQSI()[, 2] # second column
if (length(x) != length(y) || any(is.na(x)) || any(is.na(y))) {
stop("Data for QSI must have exactly two columns with matching non-missing rows.")
}
poly.qsi(list(x, y), input$Value) # calls out poly.qsi() function in QuadraticSpline.R
})
# Updates uploaded CSV data for Quadratic Spline Interpolation
observeEvent(input$file, {
req(input$file) # checks if CSV file has been uploaded
# Try-Catch method to handle error and avoid app crash
tryCatch({
data <- read.csv(input$file$datapath, header = FALSE, stringsAsFactors = FALSE)
# Checks for number of cols and data points if there are any missing values
if (ncol(data) == 2 && !any(is.na(data[[1]])) && !any(is.na(data[[2]]))) {
matrixDataQSI(data)
} else {
matrixDataQSI(NULL) # Set matrix data to NULL to avoid using incomplete data
stop("CSV file must have exactly two columns and no missing values.")
}
}, error = function(e) {
warning(paste("Error in reading CSV file:", e))
})
})
# ================== POLYNOMIAL REGRESSION ==================
# PR Reactive expression when "Generate" button is clicked
polynomialRegression <- eventReactive(input$RunPolyReg, {
req(matrixDataPoly(), input$polyFile)
data <- read.csv(input$polyFile$datapath, header = FALSE, stringsAsFactors = FALSE)
if (ncol(data) != 2) {
return(list(polyFunction = NA, polyEstimate = NA))
} else if (any(is.na(data))) {
return(list(polyFunction = NA, polyEstimate = NA))
}
if (input$polyDegree < 1) {
return(list(polyFunction = NA, polyEstimate = NA))
}
PolynomialRegression(input$polyDegree, list(data[[1]], data[[2]]), input$polyXVal)
})
# Updates uploaded CSV data for Polynomial Regression
observeEvent(input$polyFile, {
req(input$polyFile)
# Try-Catch method to handle error and avoid app crash
tryCatch({
data <- read.csv(input$polyFile$datapath, header = FALSE, stringsAsFactors = FALSE)
# Checks for number of cols and data points if there are any missing values
if (ncol(data) == 2 && !any(is.na(data[[1]])) && !any(is.na(data[[2]]))) {
matrixDataPoly(data)
} else {
matrixDataPoly(NULL) # Set matrix data to NULL to avoid using incomplete data
stop("CSV file must have exactly two columns and no missing values.")
}
}, error = function(e) {
warning(paste("Error in reading CSV file:", e))
})
})
# ================== SIMPLEX METHOD ==================
foods_data <- read.csv("FoodItem.csv") # Read the foods.csv file
food_items <- foods_data$Foods # Extract food items from column 1 (excluding the header)
food_items <- food_items[-c((length(food_items) - 1):length(food_items))]
# Create reactiveValues to store original and selected food items
foodItemsData <- reactiveValues(
original = foods_data, # Store the original data
selected = NULL
)
# Update choices in selectInput
updateSelectInput(session, "foodItems", choices = food_items)
# Check all button
observeEvent(input$checkAll, {
updateSelectInput(session, "foodItems", selected = food_items)
})
# Reset all button
observeEvent(input$resetAll, {
updateSelectInput(session, "foodItems", choices = character(0), selected = NULL) # deletes all selected
updateSelectInput(session, "foodItems", choices = food_items) # updates food items from csv
})
# Create a reactiveValues to store the Solve Simplex button state
solveSimplexClicked <- reactiveValues(clicked = FALSE)
# Reactive expression to determine whether to update optimalDiet
updateOptimalDiet <- reactive({
input$foodItems # Include input$foodItems as a dependency
if (solveSimplexClicked$clicked) {
# Check if any food items are selected
if (is.null(input$foodItems) || length(input$foodItems) == 0) {
return(data.frame(Message = "No food items selected. Please check at least one item."))
}
# Print the selected values
selected_values <- numeric()
# Extract numeric values for selected food items
for (food in input$foodItems) {
selected_values <- c(selected_values, as.numeric(foods_data[foods_data$Foods == food, -1]))
}
# Changes it back to FALSE in order to be pressed again
solveSimplexClicked <- reactiveValues(clicked = FALSE)
# Display the selected values in the table
return(data.frame(Selected_Values = selected_values))
} else {
# Solve Simplex button not clicked, return NULL
return(NULL)
}
})
# Output for optimalDiet
output$optimalDiet <- renderTable({
updateOptimalDiet()
})
# Observer for Solve Simplex button
observeEvent(input$solveSimplex, {
solveSimplexClicked$clicked <- TRUE
})
# Outputs for QSI
output$qsi.fxns = renderPrint({ quadraticSInterpolation()$qsi.fxns })
output$est = renderText({ quadraticSInterpolation()$y })
# Outputs for polynomial regression
output$polyFunction <- renderPrint({ polynomialRegression()$polynomial_string })
output$polyEstimate <- renderText({ polynomialRegression()$estimate })
}
# Run the application
shinyApp(ui = ui, server = server)