From 961d78a644a723f0eb004cae762f39051a85b762 Mon Sep 17 00:00:00 2001 From: nfrerebeau Date: Tue, 22 Oct 2024 19:20:30 +0200 Subject: [PATCH] Follow changes in nexus --- R/coda_plot.R | 66 ++++++++++++++++++++++++--------------------- R/coda_statistics.R | 40 +++++++-------------------- 2 files changed, 46 insertions(+), 60 deletions(-) diff --git a/R/coda_plot.R b/R/coda_plot.R index 2bea40c..b55e83b 100644 --- a/R/coda_plot.R +++ b/R/coda_plot.R @@ -13,24 +13,6 @@ coda_plot_ui <- function(id) { layout_sidebar( sidebar = sidebar( width = 400, - checkboxInput( - inputId = ns("order_columns"), - label = "Sort columns", - value = FALSE - ), - selectInput( - inputId = ns("order_rows"), - label = "Row order", - choices = NULL, - selected = NULL, - multiple = FALSE, - ), - checkboxInput( - inputId = ns("decreasing"), - label = "Decreasing row order", - value = FALSE - ), - hr(), checkboxInput( inputId = ns("select_major"), label = "Major elements", @@ -46,6 +28,23 @@ coda_plot_ui <- function(id) { label = "Trace elements", value = TRUE ), + hr(), + checkboxInput( + inputId = ns("order_columns"), + label = "Sort columns", + value = FALSE + ), + selectizeInput( + inputId = ns("order_rows"), + label = "Row order", + choices = NULL, selected = NULL, multiple = FALSE, + options = list(plugins = "remove_button") + ), + checkboxInput( + inputId = ns("decreasing"), + label = "Decreasing row order", + value = FALSE + ) ), # sidebar output_plot( id = ns("plot"), @@ -73,19 +72,18 @@ coda_plot_server <- function(id, x) { stopifnot(is.reactive(x)) moduleServer(id, function(input, output, session) { - ## Build barplot ----- bindEvent( observe({ freezeReactiveValue(input, "order_rows") - choices <- c("", colnames(x())) - updateSelectInput(inputId = "order_rows", choices = choices) + choices <- c("", colnames(data_bar())) + updateSelectizeInput(inputId = "order_rows", choices = choices) }), - x() + data_bar() ) - plot_bar <- reactive({ - req(x()) - col <- khroma::color(input$color_qualitative) + ## Subset ----- + data_bar <- reactive({ + req(x()) elements <- logical(ncol(x())) is_major <- nexus::is_element_major(x()) @@ -96,15 +94,23 @@ coda_plot_server <- function(id, x) { elements[which(is_minor)] <- input$select_minor elements[which(is_trace)] <- input$select_trace - if (!any(elements)) elements <- NULL + if (!any(elements)) return(x()) + x()[, elements, drop = FALSE] + }) + + ## Build barplot ----- + plot_bar <- reactive({ + req(data_bar()) + + col <- khroma::color(input$color_qualitative) + pal <- khroma::palette_color_discrete(col, domain = colnames(x())) nexus::barplot( - height = x(), - select = elements, + height = data_bar(), order_columns = input$order_columns, - order_rows = input$order_rows, + order_rows = get_value(input$order_rows), decreasing = input$decreasing, - color = khroma::palette_color_discrete(col) + color = pal ) grDevices::recordPlot() }) diff --git a/R/coda_statistics.R b/R/coda_statistics.R index 6d85418..aaeda54 100644 --- a/R/coda_statistics.R +++ b/R/coda_statistics.R @@ -39,11 +39,6 @@ coda_summary_ui <- function(id) { gt::gt_output(outputId = ns("mean")), gt::gt_output(outputId = ns("quantile")) ), - # nav_panel( - # title = "Spread", - # helpText(info_article("Hron & Kubacek", "2011", "10.1007/s00184-010-0299-3")), - # gt::gt_output(outputId = ns("spread")), - # ), nav_panel( title = "Covariance", gt::gt_output(outputId = ns("covariance")) @@ -79,11 +74,6 @@ coda_summary_server <- function(id, x) { stopifnot(is.reactive(x)) moduleServer(id, function(input, output, session) { - ## Validate ----- - data <- reactive({ - x() - }) - ## Location ----- data_loc <- reactive({ req(x()) @@ -97,19 +87,7 @@ coda_summary_server <- function(id, x) { }) ## Spread ----- - data_spread <- reactive({ - req(x()) - ## Metric variance by group - if (nexus::any_assigned(x())) { - index <- nexus::groups(x()) - s <- nexus::aggregate(x(), by = index, FUN = nexus::variance_total) - } else { - m <- nexus::variance_total(x()) - s <- matrix(m, nrow = 1, dimnames = list("", NULL)) - } - colnames(s) <- c("metric variance") - s - }) + # TODO ## Percentiles ----- data_quant <- reactive({ @@ -118,14 +96,17 @@ coda_summary_server <- function(id, x) { }) ## Histogram ----- - observeEvent(x(), { - choices <- colnames(x()) - freezeReactiveValue(input, "hist_select") - updateSelectInput(inputId = "hist_select", choices = choices) - }) + bindEvent( + observe({ + choices <- colnames(x()) + freezeReactiveValue(input, "hist_select") + updateSelectInput(inputId = "hist_select", choices = choices) + }), + x() + ) plot_hist <- reactive({ req(x()) - nexus::hist(x()[, input$hist_select, drop = FALSE], ncol = 1) + nexus::hist(x(), select = input$hist_select) grDevices::recordPlot() }) @@ -238,7 +219,6 @@ coda_summary_server <- function(id, x) { ## Download ----- output$download <- export_multiple( location = data_loc, - # spread = data_spread, quantiles = data_quant, covariance = data_cov, variation = data_var,