geposanui/R/details.R

128 lines
3.2 KiB
R
Raw Permalink Normal View History

2021-12-30 12:19:00 +01:00
#' Construct UI for the detailed results panel.
#' @noRd
2022-08-17 16:46:39 +02:00
details_ui <- function(id) {
2022-05-26 12:44:09 +02:00
verticalLayout(
2022-08-18 11:09:22 +02:00
filters_ui(NS(id, "filters")),
2022-05-26 12:44:09 +02:00
div(
style = "margin-top: 16px",
splitLayout(
cellWidths = "auto",
uiOutput(NS(id, "copy")),
2022-08-18 12:21:00 +02:00
downloadButton(
NS(id, "download"),
"Download CSV",
class = "btn-outline-primary"
)
2022-05-26 12:44:09 +02:00
)
),
div(
2022-08-18 12:21:00 +02:00
style = "margin-top: 16px; margin-bottom: 8px;",
2022-05-26 12:44:09 +02:00
DT::DTOutput(NS(id, "genes"))
2021-12-30 12:19:00 +01:00
)
2022-05-26 12:44:09 +02:00
)
2021-12-30 12:19:00 +01:00
}
#' Server for the detailed results panel.
#'
2022-08-18 09:21:48 +02:00
#' @param options Global options for the application.
2022-08-18 11:09:22 +02:00
#' @param results A reactive containing the results to be displayed.
2021-12-30 12:19:00 +01:00
#'
#' @noRd
2022-08-18 11:09:22 +02:00
details_server <- function(id, options, results) {
2022-05-26 12:44:09 +02:00
moduleServer(id, function(input, output, session) {
2022-08-18 11:09:22 +02:00
filtered_results <- filters_server("filters", results)
2022-05-26 12:44:09 +02:00
output$copy <- renderUI({
results <- filtered_results()
2021-12-30 12:19:00 +01:00
2022-05-26 12:44:09 +02:00
gene_ids <- results[, gene]
names <- results[name != "", name]
2021-12-30 12:19:00 +01:00
2022-05-26 12:44:09 +02:00
genes_text <- paste(gene_ids, collapse = "\n")
names_text <- paste(names, collapse = "\n")
2021-12-30 12:29:21 +01:00
2022-05-26 12:44:09 +02:00
splitLayout(
cellWidths = "auto",
rclipboard::rclipButton(
"copy_ids_button",
"Copy gene IDs",
genes_text,
2022-08-18 12:21:00 +02:00
icon = icon("clipboard"),
class = "btn-outline-primary"
2022-05-26 12:44:09 +02:00
),
rclipboard::rclipButton(
"copy_names_button",
"Copy gene names",
names_text,
2022-08-18 12:21:00 +02:00
icon = icon("clipboard"),
class = "btn-outline-primary"
2021-12-30 12:29:21 +01:00
)
2022-05-26 12:44:09 +02:00
)
})
2021-12-30 12:29:21 +01:00
2022-08-18 09:21:48 +02:00
methods <- options$methods
2022-05-26 17:55:40 +02:00
method_ids <- sapply(methods, function(method) method$id)
method_names <- sapply(methods, function(method) method$name)
2022-05-26 12:44:09 +02:00
columns <- c(
"rank",
"gene",
"name",
"chromosome",
"distance",
method_ids,
"score",
"percentile"
)
2021-12-30 12:29:21 +01:00
2022-05-26 12:44:09 +02:00
column_names <- c(
"",
"Gene",
"",
2022-08-18 09:54:59 +02:00
"Chr.",
2022-05-26 12:44:09 +02:00
"Distance",
method_names,
"Score",
"Percentile"
)
2021-12-30 12:29:21 +01:00
2022-05-26 12:44:09 +02:00
output$download <- downloadHandler(
filename = "geposan_filtered_results.csv",
2022-08-18 09:54:59 +02:00
content = \(file) fwrite(filtered_results()[, ..columns], file = file),
2022-05-26 12:44:09 +02:00
contentType = "text/csv"
)
output$genes <- DT::renderDT({
2022-08-18 09:54:59 +02:00
data <- filtered_results()[, ..columns]
data[, distance := glue::glue(
"{format(round(distance / 1000000, digits = 2), nsmall = 2)} Mbp"
)]
DT::datatable(
data,
2022-05-26 12:44:09 +02:00
rownames = FALSE,
colnames = column_names,
options = list(
2022-08-17 17:09:38 +02:00
rowCallback = js_link(),
2022-05-26 12:44:09 +02:00
columnDefs = list(list(visible = FALSE, targets = 2)),
pageLength = 25
)
2022-08-18 09:54:59 +02:00
) |>
DT::formatRound(c(method_ids, "score"), digits = 4) |>
DT::formatPercentage("percentile", digits = 2)
2021-12-30 12:19:00 +01:00
})
2022-05-26 12:44:09 +02:00
})
2021-12-30 12:19:00 +01:00
}
2022-08-17 17:09:38 +02:00
#' Generate a JavaScript function to replace gene IDs with Ensembl gene links.
#' @noRd
js_link <- function() {
DT::JS("function(row, data) {
let id = data[1];
var name = data[2];
if (!name) name = 'Unknown';
let url = `https://www.ensembl.org/Homo_sapiens/Gene/Summary?g=${id}`;
$('td:eq(1)', row).html(`<a href=\"${url}\" target=\"_blank\">${name}</a>`);
}")
}