From a1807691d5e0cd725277af23c73943e1193b824a Mon Sep 17 00:00:00 2001 From: Elias Projahn Date: Wed, 17 Nov 2021 22:58:55 +0100 Subject: [PATCH] Move species count to preset --- R/methods.R | 22 +++++++++------------- R/preset_editor.R | 38 +++++++++++++++++++++++++++++--------- R/server.R | 13 +------------ R/ui.R | 8 -------- 4 files changed, 39 insertions(+), 42 deletions(-) diff --git a/R/methods.R b/R/methods.R index 4a44f72..cb7ab2c 100644 --- a/R/methods.R +++ b/R/methods.R @@ -43,11 +43,9 @@ methods_ui <- function(id) { # Construct server for the methods editor. # # @param analysis The reactive containing the results to be weighted. -# @param min_n_species A reactive containing the minimum number of species to -# require for genes to be included in the ranking. # # @return A reactive containing the weighted results. -methods_server <- function(id, analysis, min_n_species) { +methods_server <- function(id, analysis) { moduleServer(id, function(input, output, session) { observeEvent(input$optimize_button, { analysis <- analysis() @@ -64,8 +62,7 @@ methods_server <- function(id, analysis, min_n_species) { analysis, method_ids, analysis$preset$reference_gene_ids, - target = input$target, - min_n_species = min_n_species() + target = input$target ) for (method_id in method_ids) { @@ -79,9 +76,12 @@ methods_server <- function(id, analysis, min_n_species) { # Observe each method's enable button and synchronise the slider state. lapply(methods, function(method) { - observeEvent(input[[method$id]], { - shinyjs::toggleState(sprintf("%s_weight", method$id)) - }, ignoreInit = TRUE) + observeEvent(input[[method$id]], + { # nolint + shinyjs::toggleState(sprintf("%s_weight", method$id)) + }, + ignoreInit = TRUE + ) }) reactive({ @@ -96,11 +96,7 @@ methods_server <- function(id, analysis, min_n_species) { } } - geposan::ranking( - analysis(), - weights, - min_n_species = min_n_species() - ) + geposan::ranking(analysis(), weights) }) }) } diff --git a/R/preset_editor.R b/R/preset_editor.R index 72f2059..dded591 100644 --- a/R/preset_editor.R +++ b/R/preset_editor.R @@ -35,6 +35,14 @@ preset_editor_ui <- function(id) { ) ) ), + sliderInput( + NS(id, "n_species"), + "Required number of species per gene", + min = 0, + max = 18, + step = 1, + value = 10 + ), selectInput( NS(id, "reference_genes"), "Reference genes", @@ -89,6 +97,7 @@ preset_editor_server <- function(id) { methods = method_ids, species_ids = species[replicative == TRUE, id], gene_ids = genes$id, + min_n_species = 10, reference_gene_ids = genes[suggested | verified == TRUE, id] )) @@ -108,6 +117,24 @@ preset_editor_server <- function(id) { } }) + species_ids <- reactive({ + if (input$species == "replicative") { + species[replicative == TRUE, id] + } else if (input$species == "all") { + species$id + } else { + input$custom_species + } + }) + + observeEvent(species_ids(), { + updateSliderInput( + session, + "n_species", + max = length(species_ids()) + ) + }) + observeEvent(input$reference_genes, { if (input$reference_genes == "custom") { updateTabsetPanel( @@ -125,14 +152,6 @@ preset_editor_server <- function(id) { }) new_preset <- reactive({ - species_ids <- if (input$species == "replicative") { - species[replicative == TRUE, id] - } else if (input$species == "all") { - species$id - } else { - input$custom_species - } - reference_gene_ids <- if (input$reference_genes == "tpeold") { genes[verified | suggested == TRUE, id] } else if (input$reference_genes == "verified") { @@ -143,8 +162,9 @@ preset_editor_server <- function(id) { geposan::preset( methods = method_ids, - species_ids = species_ids, + species_ids = species_ids(), gene_ids = genes$id, + min_n_species = input$n_species, reference_gene_ids = reference_gene_ids ) }) diff --git a/R/server.R b/R/server.R index da2f1a6..6f0bb70 100644 --- a/R/server.R +++ b/R/server.R @@ -10,15 +10,6 @@ js_link <- DT::JS("function(row, data) { server <- function(input, output, session) { preset <- preset_editor_server("preset_editor") - observe({ - species_count <- length(preset()$species_ids) - updateSliderInput( - session, - "n_species", - max = species_count - ) - }) - # Compute the results according to the preset. analysis <- reactive({ preset <- preset() @@ -37,10 +28,8 @@ server <- function(input, output, session) { analysis }) - min_n_species <- reactive(input$n_species) - # Rank the results. - ranking <- methods_server("methods", analysis, min_n_species) + ranking <- methods_server("methods", analysis) # Add gene information to the results. results <- reactive({ diff --git a/R/ui.R b/R/ui.R index afa5aa8..891a79d 100644 --- a/R/ui.R +++ b/R/ui.R @@ -7,14 +7,6 @@ ui <- fluidPage( width = 3, preset_editor_ui("preset_editor"), filters_ui("filters"), - sliderInput( - "n_species", - "Required number of species per gene", - min = 0, - max = 18, - step = 1, - value = 10 - ), methods_ui("methods") ), mainPanel(