International Rice Research Institute commited on
Update app.R
Browse files
app.R
CHANGED
|
@@ -8,6 +8,7 @@ library(emmeans)
|
|
| 8 |
library(plotly)
|
| 9 |
library(shinyWidgets)
|
| 10 |
library(ggplot2)
|
|
|
|
| 11 |
|
| 12 |
# UI with updated styling and improved plot containers
|
| 13 |
ui <- fluidPage(
|
|
@@ -359,6 +360,7 @@ ui <- fluidPage(
|
|
| 359 |
),
|
| 360 |
selectInput("lme4_selected_trait", "Select Trait for Detailed Analysis", choices = c()),
|
| 361 |
checkboxInput("lme4_include_interaction", "Include Environment:Genotype Interaction", value = FALSE),
|
|
|
|
| 362 |
div(
|
| 363 |
class = "sidebar-content",
|
| 364 |
div(
|
|
@@ -569,6 +571,7 @@ server <- function(input, output, session) {
|
|
| 569 |
message("Pheno: Reading Excel file: ", path)
|
| 570 |
tryCatch({
|
| 571 |
input_data <- read_excel(path)
|
|
|
|
| 572 |
|
| 573 |
# Validate required columns
|
| 574 |
required_cols <- c("Genotype", "Block", "Row", "Column", "Rep", "Environment")
|
|
@@ -842,6 +845,7 @@ server <- function(input, output, session) {
|
|
| 842 |
message("LME4: Reading Excel file: ", path)
|
| 843 |
tryCatch({
|
| 844 |
data <- read_excel(path)
|
|
|
|
| 845 |
|
| 846 |
required_cols <- c("Genotype", "Block", "Row", "Column", "Rep", "Environment")
|
| 847 |
missing_cols <- setdiff(required_cols, names(data))
|
|
@@ -865,6 +869,15 @@ server <- function(input, output, session) {
|
|
| 865 |
stop("No numeric columns found for traits starting from column 9")
|
| 866 |
}
|
| 867 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 868 |
combo_counts <- table(data$Environment, data$Genotype)
|
| 869 |
zero_combinations <- sum(combo_counts == 0)
|
| 870 |
if (zero_combinations > 0) {
|
|
@@ -887,12 +900,6 @@ server <- function(input, output, session) {
|
|
| 887 |
data <- data %>% filter(!Environment %in% bad_envs) %>% droplevels()
|
| 888 |
}
|
| 889 |
|
| 890 |
-
n_genotypes <- length(unique(data$Genotype))
|
| 891 |
-
n_environments <- length(unique(data$Environment))
|
| 892 |
-
if (n_genotypes < 2 || n_environments < 2) {
|
| 893 |
-
stop("Insufficient genotypes (", n_genotypes, ") or environments (", n_environments, ") for LME4 analysis")
|
| 894 |
-
}
|
| 895 |
-
|
| 896 |
message("LME4: Data summary: Rows=", nrow(data), ", Genotypes=", n_genotypes, ", Environments=", n_environments, ", Traits=", paste(numeric_cols, collapse = ", "))
|
| 897 |
return(list(data = data, traits = numeric_cols))
|
| 898 |
}, error = function(e) {
|
|
@@ -912,11 +919,17 @@ server <- function(input, output, session) {
|
|
| 912 |
}
|
| 913 |
formula <- as.formula(formula_str)
|
| 914 |
tryCatch({
|
| 915 |
-
|
| 916 |
-
|
| 917 |
-
|
| 918 |
-
message("LME4:
|
| 919 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 920 |
}
|
| 921 |
message("LME4: Successfully fitted model for ", trait)
|
| 922 |
return(model)
|
|
|
|
| 8 |
library(plotly)
|
| 9 |
library(shinyWidgets)
|
| 10 |
library(ggplot2)
|
| 11 |
+
library(blme) # Added for Bayesian modeling to handle singular fits
|
| 12 |
|
| 13 |
# UI with updated styling and improved plot containers
|
| 14 |
ui <- fluidPage(
|
|
|
|
| 360 |
),
|
| 361 |
selectInput("lme4_selected_trait", "Select Trait for Detailed Analysis", choices = c()),
|
| 362 |
checkboxInput("lme4_include_interaction", "Include Environment:Genotype Interaction", value = FALSE),
|
| 363 |
+
checkboxInput("use_blme", "Use Bayesian Model for Singular Fit", value = FALSE), # Added
|
| 364 |
div(
|
| 365 |
class = "sidebar-content",
|
| 366 |
div(
|
|
|
|
| 571 |
message("Pheno: Reading Excel file: ", path)
|
| 572 |
tryCatch({
|
| 573 |
input_data <- read_excel(path)
|
| 574 |
+
colnames(input_data) <- make.names(colnames(input_data), unique = TRUE) # Ensure unique column names
|
| 575 |
|
| 576 |
# Validate required columns
|
| 577 |
required_cols <- c("Genotype", "Block", "Row", "Column", "Rep", "Environment")
|
|
|
|
| 845 |
message("LME4: Reading Excel file: ", path)
|
| 846 |
tryCatch({
|
| 847 |
data <- read_excel(path)
|
| 848 |
+
colnames(data) <- make.names(colnames(data), unique = TRUE) # Ensure unique column names
|
| 849 |
|
| 850 |
required_cols <- c("Genotype", "Block", "Row", "Column", "Rep", "Environment")
|
| 851 |
missing_cols <- setdiff(required_cols, names(data))
|
|
|
|
| 869 |
stop("No numeric columns found for traits starting from column 9")
|
| 870 |
}
|
| 871 |
|
| 872 |
+
n_genotypes <- length(unique(data$Genotype))
|
| 873 |
+
n_environments <- length(unique(data$Environment))
|
| 874 |
+
if (n_genotypes < 3) {
|
| 875 |
+
stop("Insufficient genotypes (", n_genotypes, ") for LME4 analysis; need at least 3")
|
| 876 |
+
}
|
| 877 |
+
if (n_environments < 2) {
|
| 878 |
+
stop("Insufficient environments (", n_environments, ") for LME4 analysis; need at least 2")
|
| 879 |
+
}
|
| 880 |
+
|
| 881 |
combo_counts <- table(data$Environment, data$Genotype)
|
| 882 |
zero_combinations <- sum(combo_counts == 0)
|
| 883 |
if (zero_combinations > 0) {
|
|
|
|
| 900 |
data <- data %>% filter(!Environment %in% bad_envs) %>% droplevels()
|
| 901 |
}
|
| 902 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 903 |
message("LME4: Data summary: Rows=", nrow(data), ", Genotypes=", n_genotypes, ", Environments=", n_environments, ", Traits=", paste(numeric_cols, collapse = ", "))
|
| 904 |
return(list(data = data, traits = numeric_cols))
|
| 905 |
}, error = function(e) {
|
|
|
|
| 919 |
}
|
| 920 |
formula <- as.formula(formula_str)
|
| 921 |
tryCatch({
|
| 922 |
+
if (input$use_blme) {
|
| 923 |
+
model <- blmer(formula, data = data, na.action = na.omit,
|
| 924 |
+
control = lmerControl(check.rankX = "warn+drop.cols", optimizer = "bobyqa"))
|
| 925 |
+
message("LME4: Bayesian model fitted for ", trait)
|
| 926 |
+
} else {
|
| 927 |
+
model <- lmer(formula, data = data, na.action = na.omit,
|
| 928 |
+
control = lmerControl(check.rankX = "warn+drop.cols", optimizer = "bobyqa"))
|
| 929 |
+
if (isSingular(model)) {
|
| 930 |
+
message("LME4: Singular fit for ", trait)
|
| 931 |
+
showNotification(paste("Singular fit for", trait, "- consider using Bayesian model"), type = "warning", duration = 10)
|
| 932 |
+
}
|
| 933 |
}
|
| 934 |
message("LME4: Successfully fitted model for ", trait)
|
| 935 |
return(model)
|