Improve gene results tables

This commit is contained in:
Elias Projahn 2022-08-22 16:22:17 +02:00
parent e621761fd6
commit c97ee1ca30
4 changed files with 119 additions and 68 deletions

108
R/genes_table.R Normal file
View file

@ -0,0 +1,108 @@
#' Construct UI for the genes table.
#' @noRd
genes_table_ui <- function(id) {
verticalLayout(
div(
style = "margin-top: 16px",
splitLayout(
cellWidths = "auto",
uiOutput(NS(id, "copy")),
downloadButton(
NS(id, "download"),
"Download CSV",
class = "btn-outline-primary"
)
)
),
div(
style = "margin-top: 16px; margin-bottom: 8px;",
DT::DTOutput(NS(id, "genes"))
)
)
}
#' Server for the genes table.
#'
#' @param data A reactive containing the results to be displayed.
#'
#' @noRd
genes_table_server <- function(id, data) {
moduleServer(id, function(input, output, session) {
output$copy <- renderUI({
data <- data()
gene_ids <- data[, gene]
names <- data[hgnc_name != "", hgnc_name]
genes_text <- paste(gene_ids, collapse = "\n")
names_text <- paste(names, collapse = "\n")
splitLayout(
cellWidths = "auto",
rclipboard::rclipButton(
"copy_ids_button",
"Copy gene IDs",
genes_text,
icon = icon("clipboard"),
class = "btn-outline-primary"
),
rclipboard::rclipButton(
"copy_names_button",
"Copy HGNC symbols",
names_text,
icon = icon("clipboard"),
class = "btn-outline-primary"
)
)
})
output$download <- downloadHandler(
filename = "ubigen.csv",
content = \(file) fwrite(data(), file = file),
contentType = "text/csv"
)
output$genes <- DT::renderDT({
DT::datatable(
data()[, .(
"Gene" = glue::glue_data(
data(),
"<a href=\"https://gtexportal.org/home/gene/{hgnc_name}\" ",
"target=\"_blank\">{hgnc_name}</a>"
),
"Rank" = rank,
"Percentile" = percentile,
"Score" = score,
"Median" = median_expression,
"Mean" = mean_expression,
"Standard deviation" = sd_expression,
"Expressed" = above_zero,
"Above median" = above_median,
"Above 95%" = above_95
)],
options = list(
dom = "frtip",
pageLength = 100
),
rownames = FALSE,
escape = FALSE,
selection = "none"
) |>
DT::formatPercentage(
c(
"Percentile",
"Score",
"Expressed",
"Above median",
"Above 95%"
),
digits = 2,
) |>
DT::formatRound(c(
"Median",
"Mean",
"Standard deviation"
))
})
})
}