diff --git a/DESCRIPTION b/DESCRIPTION index c12503c..bda25ce 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -23,5 +23,6 @@ Imports: bslib, data.table, DT, + plotly, glue, shiny diff --git a/R/server.R b/R/server.R index d1969ff..8443797 100644 --- a/R/server.R +++ b/R/server.R @@ -21,9 +21,52 @@ server <- function(input, output) { data }) + output$scores_plot <- plotly::renderPlotly(scores_plot(ranked_data())) output$ranked_data <- DT::renderDataTable(genes_table(ranked_data())) } +#' Create plot showing the distribution of scores using `plotly`. +#' +#' @param ranked_data Data on genes with precomputed ranks. +#' @param ranks How may ranks the x-axis should include. If this parameter is +#' `NULL`, all ranks will be shown. +#' +#' @return A `plotly` figure for rendering. +#' @noRd +scores_plot <- function(ranked_data, ranks = 1000) { + data <- if (is.null(ranks)) { + ranked_data + } else { + ranked_data[1:ranks] + } + + ranks_label <- if (is.null(ranks)) { + "Ranks" + } else { + glue::glue("Ranks (1 to {ranks})") + } + + plotly::plot_ly() |> + plotly::add_markers( + data = data, + x = ~rank, + y = ~score, + text = ~hgnc_name, + customdata = ~percentile, + hovertemplate = paste0( + "%{text}
", + "Rank: %{x}
", + "Score: %{y:.2}
", + "Percentile: %{customdata:.2%}", + "" + ) + ) |> + plotly::layout( + xaxis = list(title = ranks_label), + yaxis = list(title = "Score") + ) +} + #' Create a displayable data table from the gene results data. #' @noRd genes_table <- function(data) { diff --git a/R/ui.R b/R/ui.R index 581ae17..bb3654f 100644 --- a/R/ui.R +++ b/R/ui.R @@ -41,7 +41,17 @@ ui <- function() { ), mainPanel( width = 9, - DT::dataTableOutput("ranked_data") + tabsetPanel( + type = "pills", + tabPanel( + title = "Distribution of scores", + plotly::plotlyOutput("scores_plot") + ), + tabPanel( + title = "Detailed results", + DT::dataTableOutput("ranked_data") + ) + ) ) ) ),