cjerzak commited on
Commit
ed41bc2
·
verified ·
1 Parent(s): f556ca2

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +69 -31
app.R CHANGED
@@ -98,7 +98,7 @@ plot_factor <- function(pi_star_list,
98
 
99
  # UI Definition
100
  ui <- fluidPage(
101
- titlePanel(
102
  "Selecting Optimal Candidate Profiles in Adversarial Environments: Case Study using a 2016 Conjoint",
103
  windowTitle = "StrategizeLab — Optimal Candidate Profiles Explorer"
104
  ),
@@ -309,39 +309,77 @@ server <- function(input, output, session) {
309
  }
310
  updateSelectInput(session, "factor", choices = factors, selected = factors[1])
311
  })
312
-
313
- # perform computation function
314
- performComputation <- function() {
315
- withProgress(message = "Retrieving results...", value = 0, {
316
- incProgress(0.2, detail = "Looking up precomputed results...")
317
- # Construct a human-readable label (as before)
318
- if (input$case_type == "Average") {
319
- label <- paste("Case=Average, Group=", input$respondent_group,
320
- ", Lambda=", input$lambda_input, sep="")
321
- lam_char <- gsub("\\.", "PT", as.character(input$lambda_input))
322
- filename <- paste0("Average_", input$respondent_group,
323
- "_lambda", lam_char, ".rds")
324
- } else {
325
- label <- paste("Case=Adversarial, Lambda=", input$lambda_input, sep="")
326
- lam_char <- gsub("\\.", "PT", as.character(input$lambda_input))
327
- filename <- paste0("Adversarial_lambda", lam_char, ".rds")
328
- }
329
- # Read the matching pre-computed .rds file from disk
330
- file_path <- file.path("AppResults", filename)
331
- Qoptimized <- readRDS(file_path)
332
- # Store the loaded results in our reactive cache
333
- cachedResults$data[[label]] <- Qoptimized
334
- incProgress(0.8, detail = "Finishing up...")
335
- # Update the choice list for previous results
336
- updateSelectInput(session, "previousResults",
337
- choices = names(cachedResults$data),
338
- selected = label)
339
- })
340
- }
341
 
342
  # Generate a new result and cache it
343
  # -- In app_ono.R, inside `server` definition --
344
- observeEvent(input$compute, performComputation(), ignoreInit = FALSE)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
345
 
346
  # Reactive to pick the result the user wants to display
347
  selectedResult <- reactive({
 
98
 
99
  # UI Definition
100
  ui <- fluidPage(
101
+ titlePanel(
102
  "Selecting Optimal Candidate Profiles in Adversarial Environments: Case Study using a 2016 Conjoint",
103
  windowTitle = "StrategizeLab — Optimal Candidate Profiles Explorer"
104
  ),
 
309
  }
310
  updateSelectInput(session, "factor", choices = factors, selected = factors[1])
311
  })
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
312
 
313
  # Generate a new result and cache it
314
  # -- In app_ono.R, inside `server` definition --
315
+ observeEvent(input$compute, {
316
+ withProgress(message = "Retrieving results...", value = 0, {
317
+ incProgress(0.2, detail = "Looking up precomputed results...")
318
+
319
+ # Construct a human-readable label (as before)
320
+ if (input$case_type == "Average") {
321
+ label <- paste("Case=Average, Group=", input$respondent_group,
322
+ ", Lambda=", input$lambda_input, sep="")
323
+ lam_char <- gsub("\\.", "PT", as.character(input$lambda_input))
324
+ filename <- paste0("Average_", input$respondent_group,
325
+ "_lambda", lam_char, ".rds")
326
+ } else {
327
+ label <- paste("Case=Adversarial, Lambda=", input$lambda_input, sep="")
328
+ lam_char <- gsub("\\.", "PT", as.character(input$lambda_input))
329
+ filename <- paste0("Adversarial_lambda", lam_char, ".rds")
330
+ }
331
+
332
+ # Read the matching pre-computed .rds file from disk
333
+ file_path <- file.path("AppResults", filename)
334
+ Qoptimized <- readRDS(file_path)
335
+
336
+ # Store the loaded results in our reactive cache
337
+ cachedResults$data[[label]] <- Qoptimized
338
+
339
+ incProgress(0.8, detail = "Finishing up...")
340
+
341
+ # Update the choice list for previous results
342
+ updateSelectInput(session, "previousResults",
343
+ choices = names(cachedResults$data),
344
+ selected = label)
345
+ })
346
+ }, ignoreInit = TRUE)
347
+
348
+ # Auto-compute results on startup with defaults
349
+ observe({
350
+ # Only run once when the app starts and inputs are available
351
+ if (!is.null(input$case_type) && !is.null(input$lambda_input)) {
352
+ # Trigger computation with default values
353
+ withProgress(message = "Loading default results...", value = 0, {
354
+ incProgress(0.2, detail = "Looking up precomputed results...")
355
+
356
+ # Use default values: Average case, All respondents, lambda 0.01
357
+ case_type <- "Average"
358
+ respondent_group <- "All"
359
+ lambda_input <- 0.01
360
+
361
+ label <- paste("Case=Average, Group=All, Lambda=0.01", sep="")
362
+ lam_char <- gsub("\\.", "PT", as.character(lambda_input))
363
+ filename <- paste0("Average_All_lambda", lam_char, ".rds")
364
+
365
+ # Read the matching pre-computed .rds file from disk
366
+ file_path <- file.path("AppResults", filename)
367
+ if (file.exists(file_path)) {
368
+ Qoptimized <- readRDS(file_path)
369
+
370
+ # Store the loaded results in our reactive cache
371
+ cachedResults$data[[label]] <- Qoptimized
372
+
373
+ incProgress(0.8, detail = "Finishing up...")
374
+
375
+ # Update the choice list for previous results
376
+ updateSelectInput(session, "previousResults",
377
+ choices = names(cachedResults$data),
378
+ selected = label)
379
+ }
380
+ })
381
+ }
382
+ })
383
 
384
  # Reactive to pick the result the user wants to display
385
  selectedResult <- reactive({