International Rice Research Institute commited on
Commit
1dd3910
·
verified ·
1 Parent(s): b740eb2

Update app.R

Browse files
Files changed (1) hide show
  1. app.R +125 -55
app.R CHANGED
@@ -8,8 +8,9 @@ library(emmeans)
8
  library(plotly)
9
  library(shinyWidgets)
10
  library(ggplot2)
 
11
 
12
- # UI with updated styling, now including Descriptive Statistics
13
  ui <- fluidPage(
14
  useShinyjs(),
15
  tags$head(
@@ -258,6 +259,20 @@ ui <- fluidPage(
258
  font-size: 14px;
259
  color: var(--dark);
260
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
261
  "))
262
  ),
263
  div(
@@ -270,6 +285,18 @@ ui <- fluidPage(
270
  ),
271
  selectInput("lme4_selected_trait", "Select Trait for Detailed Analysis", choices = c()),
272
  checkboxInput("lme4_include_interaction", "Include Environment:Genotype Interaction", value = FALSE),
 
 
 
 
 
 
 
 
 
 
 
 
273
  div(
274
  class = "sidebar-content",
275
  div(
@@ -379,7 +406,7 @@ ui <- fluidPage(
379
  ),
380
  div(
381
  id = "lme4_blues_placeholder", class = "text-center",
382
- icon("chart-bar", class = "fas", style = "font-size: 72px; color: var(--light); margin: 30px 0;"),
383
  h4("📤 Upload data to see predicted means", style = "color: var(--light);")
384
  )
385
  )
@@ -391,7 +418,7 @@ ui <- fluidPage(
391
  h2("🔗 Trait Correlations"),
392
  div(
393
  id = "lme4_correlations_content",
394
- div(class = "chart-container", plotlyOutput("lme4_correlation_plot", height = "500px")),
395
  div(class = "table-container", DTOutput("lme4_cor_table")),
396
  div(
397
  class = "insights",
@@ -423,27 +450,54 @@ server <- function(input, output, session) {
423
  cor_matrix = NULL,
424
  cor_table = NULL,
425
  descriptives = NULL,
426
- debug_log = NULL
 
 
 
 
 
 
427
  )
428
 
429
- # Plot formatting constants
430
- PLOT_FONT <- list(family = "Poppins", size = 14, color = "#2D3748")
431
- PLOT_COLORS <- c("#FFB6C1", "#FFDAB9", "#B5EAD7", "#E6E6FA", "#FFFACD", "#FF9999")
432
- PLOT_LAYOUT <- list(
433
- plot_bgcolor = "transparent",
434
- paper_bgcolor = "transparent",
435
- margin = list(l = 80, r = 80, t = 100, b = 100),
436
- font = PLOT_FONT,
437
- showlegend = TRUE,
438
- legend = list(orientation = "h", y = -0.2, x = 0.5, xanchor = "center")
439
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
440
 
441
  # LME4: Process data
442
  lme4_process_data <- function(path) {
443
  message("LME4: Reading Excel file: ", path)
444
  tryCatch({
445
  data <- read_excel(path)
446
- colnames(data) <- make.names(colnames(data), unique = TRUE) # Ensure unique column names
447
 
448
  required_cols <- c("Genotype", "Block", "Row", "Column", "Rep", "Environment")
449
  missing_cols <- setdiff(required_cols, names(data))
@@ -590,7 +644,7 @@ server <- function(input, output, session) {
590
  lme4_models <- list()
591
  blues_list <- list()
592
 
593
- total_steps <- length(rv_lme4$traits) * 2 + 2 # Extra steps for descriptives and correlations
594
  step_increment <- 1 / total_steps
595
 
596
  for (trait in rv_lme4$traits) {
@@ -646,7 +700,6 @@ server <- function(input, output, session) {
646
  rv_lme4$blues_all <- bind_rows(blues_list) %>% filter(!is.na(predicted.value))
647
  rv_lme4$debug_log <- debug_log
648
 
649
- # Show contents for all tabs
650
  shinyjs::hide("lme4_blues_placeholder")
651
  shinyjs::show("lme4_blues_content")
652
  shinyjs::hide("lme4_correlations_placeholder")
@@ -681,63 +734,80 @@ server <- function(input, output, session) {
681
  if (nrow(data) == 0 || all(is.na(data$predicted.value))) {
682
  return(plot_ly() %>%
683
  layout(
684
- title = list(text = paste("No Valid Predicted Means for", rv_lme4$trait), font = PLOT_FONT),
685
  annotations = list(
686
  text = "No valid data for plotting predicted means.",
687
  x = 0.5, y = 0.5, xref = "paper", yref = "paper",
688
  showarrow = FALSE, font = list(size = 12, color = "#2D3748")
689
  ),
690
- PLOT_LAYOUT
691
  ))
692
  }
693
  plot_ly(data, x = ~Environment, y = ~predicted.value, color = ~Genotype,
694
  type = 'scatter', mode = 'lines+markers',
695
  marker = list(size = 8),
696
  line = list(width = 2),
697
- colors = PLOT_COLORS,
698
  text = ~paste("Genotype:", Genotype, "<br>Value:", round(predicted.value, 2)),
699
  hoverinfo = "text") %>%
700
  layout(
701
  title = list(text = paste("Predicted Means (BLUEs) for", rv_lme4$trait), font = list(size = 18, family = "Poppins", color = "#2D3748")),
702
- xaxis = list(title = "Environment", tickangle = 45, titlefont = PLOT_FONT, tickfont = PLOT_FONT),
703
- yaxis = list(title = "Predicted Value", titlefont = PLOT_FONT, tickfont = PLOT_FONT),
704
- PLOT_LAYOUT,
705
- hoverlabel = list(bgcolor = "white", font = PLOT_FONT)
706
  )
707
  })
708
 
 
709
  output$lme4_correlation_plot <- renderPlotly({
710
- req(rv_lme4$cor_matrix)
711
- if (ncol(rv_lme4$cor_matrix) < 2 || all(is.na(rv_lme4$cor_matrix))) {
712
  return(plot_ly() %>%
713
  layout(
714
- title = list(text = "No Valid Correlation Data", font = PLOT_FONT),
715
  annotations = list(
716
- text = "Correlation plot requires at least two valid traits.",
717
  x = 0.5, y = 0.5, xref = "paper", yref = "paper",
718
  showarrow = FALSE, font = list(size = 12, color = "#2D3748")
719
  ),
720
- PLOT_LAYOUT
721
  ))
722
  }
723
- plot_ly(
724
- x = colnames(rv_lme4$cor_matrix),
725
- y = rownames(rv_lme4$cor_matrix),
726
- z = rv_lme4$cor_matrix,
727
- type = "heatmap",
728
- colorscale = list(c(-1, 0, 1), c("#FFB6C1", "white", "#B5EAD7")),
729
- showscale = TRUE,
730
- colorbar = list(title = "Correlation", titlefont = PLOT_FONT, tickfont = PLOT_FONT),
731
- text = ~round(rv_lme4$cor_matrix, 2),
732
- hoverinfo = "x+y+z"
733
- ) %>%
734
- layout(
735
- title = list(text = "Trait Correlation Matrix", font = list(size = 18, family = "Poppins", color = "#2D3748")),
736
- xaxis = list(title = "", tickangle = 45, titlefont = PLOT_FONT, tickfont = PLOT_FONT),
737
- yaxis = list(title = "", titlefont = PLOT_FONT, tickfont = PLOT_FONT),
738
- PLOT_LAYOUT,
739
- hoverlabel = list(bgcolor = "white", font = PLOT_FONT)
740
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
741
  })
742
 
743
  # LME4: Descriptive Statistics Plot (Boxplot by Environment for selected trait)
@@ -747,24 +817,24 @@ server <- function(input, output, session) {
747
  if (all(is.na(trait_data)) || length(unique(rv_lme4$data$Environment)) < 2) {
748
  return(plot_ly() %>%
749
  layout(
750
- title = list(text = paste("No Valid Data for", rv_lme4$trait), font = PLOT_FONT),
751
  annotations = list(
752
  text = "No valid data for boxplot.",
753
  x = 0.5, y = 0.5, xref = "paper", yref = "paper",
754
  showarrow = FALSE, font = list(size = 12, color = "#2D3748")
755
  ),
756
- PLOT_LAYOUT
757
  ))
758
  }
759
  plot_ly(rv_lme4$data, x = ~Environment, y = rv_lme4$data[[rv_lme4$trait]], type = 'box',
760
- color = ~Environment, colors = PLOT_COLORS,
761
  hoverinfo = "y+name") %>%
762
  layout(
763
  title = list(text = paste("Distribution of", rv_lme4$trait, "by Environment"), font = list(size = 18, family = "Poppins", color = "#2D3748")),
764
- xaxis = list(title = "Environment", titlefont = PLOT_FONT, tickfont = PLOT_FONT),
765
- yaxis = list(title = paste(rv_lme4$trait, "Value"), titlefont = PLOT_FONT, tickfont = PLOT_FONT),
766
- PLOT_LAYOUT,
767
- hoverlabel = list(bgcolor = "white", font = PLOT_FONT)
768
  )
769
  })
770
 
 
8
  library(plotly)
9
  library(shinyWidgets)
10
  library(ggplot2)
11
+ library(GGally)
12
 
13
+ # UI with updated styling, now including Descriptive Statistics and color options
14
  ui <- fluidPage(
15
  useShinyjs(),
16
  tags$head(
 
259
  font-size: 14px;
260
  color: var(--dark);
261
  }
262
+
263
+ .color-picker-container {
264
+ background: #F9F9F9;
265
+ padding: 15px;
266
+ border-radius: 8px;
267
+ margin-bottom: 15px;
268
+ border: 1px solid #E0E0E0;
269
+ }
270
+
271
+ .color-picker-title {
272
+ font-weight: 600;
273
+ margin-bottom: 10px;
274
+ color: var(--primary);
275
+ }
276
  "))
277
  ),
278
  div(
 
285
  ),
286
  selectInput("lme4_selected_trait", "Select Trait for Detailed Analysis", choices = c()),
287
  checkboxInput("lme4_include_interaction", "Include Environment:Genotype Interaction", value = FALSE),
288
+
289
+ # Color customization options
290
+ div(
291
+ class = "color-picker-container",
292
+ div(class = "color-picker-title", "🎨 Plot Colors"),
293
+ colourInput("color_primary", "Primary Color", value = "#1E3A8A"),
294
+ colourInput("color_secondary", "Secondary Color", value = "#6B8E23"),
295
+ colourInput("color_accent", "Accent Color", value = "#4169E1"),
296
+ colourInput("color_highlight", "Highlight Color", value = "#FF8C00"),
297
+ actionButton("apply_colors", "Apply Colors", class = "btn-primary", style = "width: 100%;")
298
+ ),
299
+
300
  div(
301
  class = "sidebar-content",
302
  div(
 
406
  ),
407
  div(
408
  id = "lme4_blues_placeholder", class = "text-center",
409
+ icon("chart-bar", class = "fas", style = "font-size: 72px; color: var(--light; margin: 30px 0;"),
410
  h4("📤 Upload data to see predicted means", style = "color: var(--light);")
411
  )
412
  )
 
418
  h2("🔗 Trait Correlations"),
419
  div(
420
  id = "lme4_correlations_content",
421
+ div(class = "chart-container", plotlyOutput("lme4_correlation_plot", height = "600px")),
422
  div(class = "table-container", DTOutput("lme4_cor_table")),
423
  div(
424
  class = "insights",
 
450
  cor_matrix = NULL,
451
  cor_table = NULL,
452
  descriptives = NULL,
453
+ debug_log = NULL,
454
+ plot_colors = list(
455
+ primary = "#1E3A8A",
456
+ secondary = "#6B8E23",
457
+ accent = "#4169E1",
458
+ highlight = "#FF8C00"
459
+ )
460
  )
461
 
462
+ # Update colors when apply button is clicked
463
+ observeEvent(input$apply_colors, {
464
+ rv_lme4$plot_colors$primary <- input$color_primary
465
+ rv_lme4$plot_colors$secondary <- input$color_secondary
466
+ rv_lme4$plot_colors$accent <- input$color_accent
467
+ rv_lme4$plot_colors$highlight <- input$color_highlight
468
+
469
+ showNotification("Colors updated! Refresh plots to see changes.", type = "message")
470
+ })
471
+
472
+ # Generate color palette based on selected colors
473
+ get_color_palette <- function() {
474
+ c(
475
+ rv_lme4$plot_colors$primary,
476
+ rv_lme4$plot_colors$secondary,
477
+ rv_lme4$plot_colors$accent,
478
+ rv_lme4$plot_colors$highlight,
479
+ "#B5EAD7", "#E6E6FA", "#FFFACD", "#FF9999"
480
+ )
481
+ }
482
+
483
+ # Plot formatting with dynamic colors
484
+ get_plot_layout <- function() {
485
+ list(
486
+ plot_bgcolor = "transparent",
487
+ paper_bgcolor = "transparent",
488
+ margin = list(l = 80, r = 80, t = 100, b = 100),
489
+ font = list(family = "Poppins", size = 14, color = "#2D3748"),
490
+ showlegend = TRUE,
491
+ legend = list(orientation = "h", y = -0.2, x = 0.5, xanchor = "center")
492
+ )
493
+ }
494
 
495
  # LME4: Process data
496
  lme4_process_data <- function(path) {
497
  message("LME4: Reading Excel file: ", path)
498
  tryCatch({
499
  data <- read_excel(path)
500
+ colnames(data) <- make.names(colnames(data), unique = TRUE)
501
 
502
  required_cols <- c("Genotype", "Block", "Row", "Column", "Rep", "Environment")
503
  missing_cols <- setdiff(required_cols, names(data))
 
644
  lme4_models <- list()
645
  blues_list <- list()
646
 
647
+ total_steps <- length(rv_lme4$traits) * 2 + 2
648
  step_increment <- 1 / total_steps
649
 
650
  for (trait in rv_lme4$traits) {
 
700
  rv_lme4$blues_all <- bind_rows(blues_list) %>% filter(!is.na(predicted.value))
701
  rv_lme4$debug_log <- debug_log
702
 
 
703
  shinyjs::hide("lme4_blues_placeholder")
704
  shinyjs::show("lme4_blues_content")
705
  shinyjs::hide("lme4_correlations_placeholder")
 
734
  if (nrow(data) == 0 || all(is.na(data$predicted.value))) {
735
  return(plot_ly() %>%
736
  layout(
737
+ title = list(text = paste("No Valid Predicted Means for", rv_lme4$trait), font = list(family = "Poppins")),
738
  annotations = list(
739
  text = "No valid data for plotting predicted means.",
740
  x = 0.5, y = 0.5, xref = "paper", yref = "paper",
741
  showarrow = FALSE, font = list(size = 12, color = "#2D3748")
742
  ),
743
+ get_plot_layout()
744
  ))
745
  }
746
  plot_ly(data, x = ~Environment, y = ~predicted.value, color = ~Genotype,
747
  type = 'scatter', mode = 'lines+markers',
748
  marker = list(size = 8),
749
  line = list(width = 2),
750
+ colors = get_color_palette(),
751
  text = ~paste("Genotype:", Genotype, "<br>Value:", round(predicted.value, 2)),
752
  hoverinfo = "text") %>%
753
  layout(
754
  title = list(text = paste("Predicted Means (BLUEs) for", rv_lme4$trait), font = list(size = 18, family = "Poppins", color = "#2D3748")),
755
+ xaxis = list(title = "Environment", tickangle = 45, titlefont = list(family = "Poppins"), tickfont = list(family = "Poppins")),
756
+ yaxis = list(title = "Predicted Value", titlefont = list(family = "Poppins"), tickfont = list(family = "Poppins")),
757
+ get_plot_layout(),
758
+ hoverlabel = list(bgcolor = "white", font = list(family = "Poppins"))
759
  )
760
  })
761
 
762
+ # Scatter plot matrix for correlations
763
  output$lme4_correlation_plot <- renderPlotly({
764
+ req(rv_lme4$data, rv_lme4$traits)
765
+ if (length(rv_lme4$traits) < 2) {
766
  return(plot_ly() %>%
767
  layout(
768
+ title = list(text = "Need at least 2 traits for correlation plot", font = list(family = "Poppins")),
769
  annotations = list(
770
+ text = "Upload data with at least 2 numeric traits.",
771
  x = 0.5, y = 0.5, xref = "paper", yref = "paper",
772
  showarrow = FALSE, font = list(size = 12, color = "#2D3748")
773
  ),
774
+ get_plot_layout()
775
  ))
776
  }
777
+
778
+ # Prepare data for scatter plot matrix
779
+ plot_data <- rv_lme4$data %>%
780
+ select(all_of(rv_lme4$traits)) %>%
781
+ na.omit()
782
+
783
+ if (nrow(plot_data) == 0) {
784
+ return(plot_ly() %>%
785
+ layout(
786
+ title = list(text = "No valid data for correlation plot", font = list(family = "Poppins")),
787
+ annotations = list(
788
+ text = "No complete cases found for the selected traits.",
789
+ x = 0.5, y = 0.5, xref = "paper", yref = "paper",
790
+ showarrow = FALSE, font = list(size = 12, color = "#2D3748")
791
+ ),
792
+ get_plot_layout()
793
+ ))
794
+ }
795
+
796
+ # Create scatter plot matrix
797
+ fig <- plot_ly(type = 'splom', dimensions = lapply(names(plot_data), function(col) {
798
+ list(label = col, values = plot_data[[col]])
799
+ }))
800
+
801
+ fig <- fig %>% layout(
802
+ title = list(text = "Trait Correlation Scatter Plot Matrix", font = list(size = 18, family = "Poppins", color = "#2D3748")),
803
+ dragmode = 'select',
804
+ hovermode = 'closest',
805
+ plot_bgcolor = 'transparent',
806
+ paper_bgcolor = 'transparent',
807
+ font = list(family = "Poppins", size = 12)
808
+ )
809
+
810
+ fig
811
  })
812
 
813
  # LME4: Descriptive Statistics Plot (Boxplot by Environment for selected trait)
 
817
  if (all(is.na(trait_data)) || length(unique(rv_lme4$data$Environment)) < 2) {
818
  return(plot_ly() %>%
819
  layout(
820
+ title = list(text = paste("No Valid Data for", rv_lme4$trait), font = list(family = "Poppins")),
821
  annotations = list(
822
  text = "No valid data for boxplot.",
823
  x = 0.5, y = 0.5, xref = "paper", yref = "paper",
824
  showarrow = FALSE, font = list(size = 12, color = "#2D3748")
825
  ),
826
+ get_plot_layout()
827
  ))
828
  }
829
  plot_ly(rv_lme4$data, x = ~Environment, y = rv_lme4$data[[rv_lme4$trait]], type = 'box',
830
+ color = ~Environment, colors = get_color_palette(),
831
  hoverinfo = "y+name") %>%
832
  layout(
833
  title = list(text = paste("Distribution of", rv_lme4$trait, "by Environment"), font = list(size = 18, family = "Poppins", color = "#2D3748")),
834
+ xaxis = list(title = "Environment", titlefont = list(family = "Poppins"), tickfont = list(family = "Poppins")),
835
+ yaxis = list(title = paste(rv_lme4$trait, "Value"), titlefont = list(family = "Poppins"), tickfont = list(family = "Poppins")),
836
+ get_plot_layout(),
837
+ hoverlabel = list(bgcolor = "white", font = list(family = "Poppins"))
838
  )
839
  })
840