Add score distribution plot

This commit is contained in:
Elias Projahn 2022-05-12 12:21:09 +02:00
parent 276730f217
commit 1a47b30dbf
3 changed files with 55 additions and 1 deletions

View file

@ -23,5 +23,6 @@ Imports:
bslib,
data.table,
DT,
plotly,
glue,
shiny

View file

@ -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(
"<b>%{text}</b><br>",
"Rank: %{x}<br>",
"Score: %{y:.2}<br>",
"Percentile: %{customdata:.2%}",
"<extra></extra>"
)
) |>
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) {

12
R/ui.R
View file

@ -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")
)
)
)
)
),