diff --git a/R/server.R b/R/server.R index 53121b9..c2d1508 100644 --- a/R/server.R +++ b/R/server.R @@ -1,41 +1,67 @@ #' Server implementing the main user interface. #' @noRd server <- function(input, output) { - output$all_data <- DT::renderDataTable({ - data <- ubigen::genes[, .( - "Gene" = glue::glue_data( - ubigen::genes, - "{hgnc_name}" - ), - "Rank" = rank, - "Score" = score, - "Median" = median_expression, - "Mean" = mean_expression, - "Standard deviation" = sd_expression, - "Expressed" = above_zero, - "Above 50 TPM" = above_threshold, - "Above median" = above_median, - "Above 95%" = above_95 - )] + output$ranked_data <- DT::renderDataTable({ + total_weight <- abs(input$above_median) + + abs(input$mean_expression) + + abs(input$sd_expression) - DT::datatable( + data <- data.table::copy(ubigen::genes) + + data[, score := + (input$above_median * above_median + + input$mean_expression * mean_expression_normalized + + input$sd_expression * sd_expression_normalized) / + total_weight] + + data.table::setorder(data, -score) + data[, rank := .I] + + genes_table(data) + }) + + output$all_data <- DT::renderDataTable(genes_table(ubigen::genes)) +} + +#' Create a displayable data table from the gene results data. +#' @noRd +genes_table <- function(data) { + data <- data[, .( + "Gene" = glue::glue_data( data, - options = list(pageLength = 100), - rownames = FALSE, - escape = FALSE - ) |> - DT::formatPercentage(c( + "{hgnc_name}" + ), + "Rank" = rank, + "Score" = score, + "Median" = median_expression, + "Mean" = mean_expression, + "Standard deviation" = sd_expression, + "Expressed" = above_zero, + "Above 50 TPM" = above_threshold, + "Above median" = above_median, + "Above 95%" = above_95 + )] + + DT::datatable( + data, + options = list(pageLength = 100), + rownames = FALSE, + escape = FALSE + ) |> + DT::formatPercentage( + c( "Score", "Expressed", "Above 50 TPM", "Above median", "Above 95%" - )) |> - DT::formatRound(c( - "Median", - "Mean", - "Standard deviation" - )) - }) + ), + digits = 2, + ) |> + DT::formatRound(c( + "Median", + "Mean", + "Standard deviation" + )) } diff --git a/R/ui.R b/R/ui.R index 112356c..a45e025 100644 --- a/R/ui.R +++ b/R/ui.R @@ -13,9 +13,35 @@ ui <- function() { sidebarLayout( sidebarPanel( width = 3, + h3("Criteria"), + sliderInput( + "above_median", + "Expressed above median", + min = -1.0, + max = 1.0, + step = 0.01, + value = 1.0 + ), + sliderInput( + "mean_expression", + "Mean expression", + min = -1.0, + max = 1.0, + step = 0.01, + value = 1.0 + ), + sliderInput( + "sd_expression", + "Standard deviation", + min = -1.0, + max = 1.0, + step = 0.01, + value = -1.0 + ) ), mainPanel( width = 9, + DT::dataTableOutput("ranked_data") ) ) ),