geposanui/R/methods.R

177 lines
4.7 KiB
R
Raw Normal View History

2021-10-20 15:34:52 +02:00
# Construct UI for the methods editor.
2021-10-19 16:44:29 +02:00
methods_ui <- function(id) {
2022-05-26 12:44:09 +02:00
verticalLayout(
h3("Methods"),
selectInput(
NS(id, "optimization_genes"),
"Genes to optimize for",
choices = list(
"Reference genes" = "reference",
"Comparison genes" = "comparison"
)
),
selectInput(
NS(id, "optimization_target"),
"Optimization target",
choices = list(
2022-08-17 16:18:25 +02:00
"Mean squared error" = "mse",
"Mean percentile" = "mean",
"Median percentile" = "median",
"Worst percentile" = "worst",
"Best percentile" = "best",
2022-05-26 12:44:09 +02:00
"Customize weights" = "custom"
)
),
uiOutput(NS(id, "method_sliders"))
2022-05-26 12:44:09 +02:00
)
2021-10-19 16:44:29 +02:00
}
2021-10-20 15:34:52 +02:00
# Construct server for the methods editor.
#
# @param analysis The reactive containing the results to be weighted.
#
# @return A reactive containing the weighted results.
methods_server <- function(id, analysis, comparison_gene_ids) {
2022-05-26 12:44:09 +02:00
moduleServer(id, function(input, output, session) {
output$method_sliders <- renderUI({
lapply(analysis()$preset$methods, function(method) {
verticalLayout(
checkboxInput(
session$ns(method$id),
span(
method$description,
class = "control-label"
),
value = TRUE
),
sliderInput(
session$ns(sprintf("%s_weight", method$id)),
NULL,
min = -1.0,
max = 1.0,
step = 0.01,
value = 1.0
)
2022-05-26 12:44:09 +02:00
)
})
})
method_observers <- list()
method_listeners <- list()
observe({
for (method_observer in method_observers) {
destroy(method_observer)
}
for (method_listener in method_listeners) {
shinyjs::removeEvent(method_listener)
}
method_observers <- lapply(analysis()$preset$methods, function(method) {
observeEvent(input[[method$id]], {
shinyjs::toggleState(
sprintf("%s_weight", method$id),
condition = input[[method$id]]
)
})
2022-05-26 12:44:09 +02:00
})
method_listeners <- lapply(analysis()$preset$methods, function(method) {
shinyjs::onclick(sprintf("%s_weight", method$id), {
updateSelectInput(
session,
"optimization_target",
selected = "custom"
)
})
})
for (method in analysis()$preset$methods) {
method_observer <-
method_observers <- c(method_observers, method_observer)
method_listener <- shinyjs::onclick(sprintf("%s_weight", method$id), {
updateSelectInput(
session,
"optimization_target",
selected = "custom"
)
})
method_listeners <- c(method_listeners, method_listener)
}
}) |> bindEvent(analysis())
2021-10-19 16:44:29 +02:00
2022-05-26 12:44:09 +02:00
# This reactive will always contain the currently selected optimization
# gene IDs in a normalized form.
optimization_gene_ids <- reactive({
gene_ids <- if (input$optimization_genes == "comparison") {
comparison_gene_ids()
} else {
analysis()$preset$reference_gene_ids
}
2022-05-26 12:44:09 +02:00
sort(unique(gene_ids))
})
2022-05-26 12:44:09 +02:00
# This reactive will always contain the optimal weights according to
# the selected parameters.
optimal_weights <- reactive({
withProgress(message = "Optimizing weights", {
setProgress(0.2)
2022-05-26 12:44:09 +02:00
included_methods <- NULL
for (method in analysis()$preset$methods) {
if (!is.null(input[[method$id]])) {
if (input[[method$id]]) {
included_methods <- c(included_methods, method$id)
}
2022-05-26 12:44:09 +02:00
}
}
2021-12-16 13:52:14 +01:00
2022-05-26 12:44:09 +02:00
geposan::optimal_weights(
analysis(),
included_methods,
optimization_gene_ids(),
target = input$optimization_target
)
2022-05-26 12:44:09 +02:00
})
}) |> bindCache(
analysis(),
optimization_gene_ids(),
sapply(analysis()$preset$methods, function(method) input[[method$id]]),
2022-05-26 12:44:09 +02:00
input$optimization_target
)
2022-05-26 12:44:09 +02:00
reactive({
weights <- NULL
2021-12-16 13:52:14 +01:00
2022-05-26 12:44:09 +02:00
if (length(optimization_gene_ids()) < 1 |
input$optimization_target == "custom") {
for (method in analysis()$preset$methods) {
if (!is.null(input[[method$id]])) {
if (input[[method$id]]) {
weight <- input[[sprintf("%s_weight", method$id)]]
weights[[method$id]] <- weight
}
2022-05-26 12:44:09 +02:00
}
}
} else {
weights <- optimal_weights()
2021-12-16 13:52:14 +01:00
2022-05-26 12:44:09 +02:00
for (method_id in names(weights)) {
updateSliderInput(
session,
sprintf("%s_weight", method_id),
value = weights[[method_id]]
)
}
}
2021-10-19 16:44:29 +02:00
2022-05-26 12:44:09 +02:00
geposan::ranking(analysis(), weights)
2021-10-19 16:44:29 +02:00
})
2022-05-26 12:44:09 +02:00
})
2021-10-19 16:44:29 +02:00
}