@@ -265,6 +265,7 @@ mod_tool_server2 <- function(id, rv) {
265265 shinyjs :: hide(" analysis_results" )
266266 shinyjs :: show(" analysis_progress" )
267267 shinyjs :: html(" analysis_console" , " " )
268+ shinyjs :: disable(" btn_analysis_results" )
268269 shinyWidgets :: updateProgressBar(
269270 session = session ,
270271 id = " analysis_progress_bar" ,
@@ -288,6 +289,7 @@ mod_tool_server2 <- function(id, rv) {
288289 }
289290 ),
290291 error = function (e ) {
292+ shinyjs :: disable(" btn_analysis_results" )
291293 shinyjs :: hide(" analysis_progress" )
292294 shinyjs :: toggle(" analysis_results" , condition = ! is.null(rv $ analysis $ result ))
293295 shinyjs :: toggle(" analysis_no_result" , condition = is.null(rv $ analysis $ result ))
@@ -313,11 +315,20 @@ mod_tool_server2 <- function(id, rv) {
313315 rv $ analysis $ dims <- dims_sel
314316 rv $ analysis $ entity <- input $ analysis_sel_entity
315317
316- shinyjs :: hide( " analysis_progress " )
318+ shinyjs :: enable( " btn_analysis_results " )
317319 }
318320 })
319321 # # ++ ##
320322
323+ # # ++ ##
324+ observeEvent(input $ btn_analysis_results , {
325+ req(rv $ analysis $ result )
326+ shinyjs :: hide(" analysis_progress" )
327+ shinyjs :: hide(" analysis_no_result" )
328+ shinyjs :: show(" analysis_results" )
329+ })
330+ # # ++ ##
331+
321332 # # $$$
322333
323334
@@ -611,6 +622,51 @@ mod_tool_server2 <- function(id, rv) {
611622 p
612623 }
613624
625+ # # ++ ##
626+ filtered_analysis_df <- function (source = c(" MEANS" , " TOTALS" )) {
627+ source <- match.arg(source )
628+
629+ req(rv $ analysis $ result )
630+
631+ df <- rv $ analysis $ result [[source ]]
632+ filters <- tryCatch(get_filter_vals(), error = function (e ) NULL )
633+
634+ if (is.null(filters )) {
635+ return (df )
636+ }
637+
638+ for (nm in names(filters )) {
639+ vals <- filters [[nm ]]
640+ if (length(vals ) > 0 ) {
641+ df <- dplyr :: filter(df , .data [[nm ]] %in% vals )
642+ }
643+ }
644+
645+ df
646+ }
647+
648+ table_display_df <- function (source = c(" MEANS" , " TOTALS" )) {
649+ source <- match.arg(source )
650+
651+ df <- filtered_analysis_df(source )
652+ measure_names <- rv $ analysis $ measures_meta | > dplyr :: pull(" name" )
653+ selected_measures <- input $ analysis_table_measures %|| % measure_names
654+
655+ measure_cols <- names(df )[
656+ purrr :: map_lgl(
657+ names(df ),
658+ \(col ) any(stringr :: str_detect(col , paste0(" ^" , selected_measures , " ($|_)" )))
659+ )
660+ ]
661+
662+ keep_cols <- unique(c(setdiff(names(df ), unlist(
663+ purrr :: map(measure_names , \(m ) names(df )[stringr :: str_detect(names(df ), paste0(" ^" , m , " ($|_)" ))])
664+ )), measure_cols ))
665+
666+ dplyr :: select(df , dplyr :: all_of(keep_cols ))
667+ }
668+ # # ++ ##
669+
614670 # # . + Update plot selectors when a new result arrives ------
615671 observeEvent(rv $ analysis $ result , {
616672 req(rv $ analysis $ result , rv $ analysis $ dim_meta , rv $ analysis $ measures_meta )
@@ -631,11 +687,46 @@ mod_tool_server2 <- function(id, rv) {
631687 updateSelectInput(session , " plot_measure" , choices = meas_choices , selected = meas_choices [1 ])
632688 updateSelectInput(session , " plot_fill" , choices = optional_choices , selected = " " )
633689 updateSelectInput(session , " plot_facet" , choices = optional_choices , selected = " " )
690+ shinyWidgets :: updatePickerInput(
691+ session = session ,
692+ inputId = " analysis_table_measures" ,
693+ choices = meas_choices ,
694+ selected = meas_meta $ name
695+ )
696+ })
634697
635- shinyjs :: hide(" analysis_no_result" )
636- shinyjs :: show(" analysis_results" )
698+ # # ++ ##
699+ output $ analysis_table <- DT :: renderDT({
700+ req(rv $ analysis $ result , input $ analysis_table_source )
701+
702+ DT :: datatable(
703+ table_display_df(input $ analysis_table_source ),
704+ rownames = FALSE ,
705+ filter = " top" ,
706+ extensions = " Buttons" ,
707+ options = list (
708+ scrollX = TRUE ,
709+ pageLength = 10 ,
710+ dom = " Bfrtip" ,
711+ buttons = c(" copy" )
712+ )
713+ )
637714 })
638715
716+ output $ analysis_table_download <- downloadHandler(
717+ filename = function () {
718+ paste0(" analysis_" , tolower(input $ analysis_table_source %|| % " means" ), " .csv" )
719+ },
720+ content = function (file ) {
721+ utils :: write.csv(
722+ filtered_analysis_df(input $ analysis_table_source %|| % " MEANS" ),
723+ file ,
724+ row.names = FALSE
725+ )
726+ }
727+ )
728+ # # ++ ##
729+
639730 # # $$$
640731 # # . + Dimension filters -------------------------------------------------
641732 # # One multi-select per dimension used in the analysis (all dims, regardless
@@ -699,7 +790,7 @@ mod_tool_server2 <- function(id, rv) {
699790 output $ analysis_plot_means <- renderPlot({
700791 req(rv $ analysis $ result , input $ plot_dim , input $ plot_measure )
701792 make_bar_plot(
702- df = rv $ analysis $ result $ MEANS ,
793+ df = filtered_analysis_df( " MEANS" ) ,
703794 x_dim = input $ plot_dim ,
704795 measure = input $ plot_measure ,
705796 fill_col = input $ plot_fill ,
@@ -717,7 +808,7 @@ mod_tool_server2 <- function(id, rv) {
717808 output $ analysis_plot_totals <- renderPlot({
718809 req(rv $ analysis $ result , input $ plot_dim , input $ plot_measure )
719810 make_bar_plot(
720- df = rv $ analysis $ result $ TOTALS ,
811+ df = filtered_analysis_df( " TOTALS" ) ,
721812 x_dim = input $ plot_dim ,
722813 measure = input $ plot_measure ,
723814 fill_col = input $ plot_fill ,
0 commit comments