From b5b08c44090a85cd2a8efd5b3a17e018b09484c9 Mon Sep 17 00:00:00 2001 From: Kannan Date: Tue, 10 Dec 2024 18:55:49 +0530 Subject: [PATCH] data export button for the graph in the Bivariate Correlation tab #322 --- app/auxiliary/plots.R | 14 ++++++++-- app/server.R | 65 ++++++++++++++++++++++++++++++++++++++++++- app/ui.R | 12 +++++++- 3 files changed, 87 insertions(+), 4 deletions(-) diff --git a/app/auxiliary/plots.R b/app/auxiliary/plots.R index 93b66fc..a3d7a08 100644 --- a/app/auxiliary/plots.R +++ b/app/auxiliary/plots.R @@ -1786,7 +1786,17 @@ static_scatter <- "y: ", {{yvar}} , "
", "
" ) ) - + + + sc_data<-data %>% + select(country_code, country_name, income_group, region, country_group, x, y) + + sc_data<- sc_data %>% + rename( + !!x_scatter := x, # Rename 'x' to the value in x_scatter + !!y_scatter := y # Rename 'y' to the value in y_scatter + ) + #PLOTTING THE SCATTER PLOT sc_plot <- ggplot( data, @@ -1867,7 +1877,7 @@ static_scatter <- }else sc_plot <- sc_plot - return(sc_plot) + return(list(sc_plot = sc_plot, sc_data = sc_data)) } interactive_scatter <- diff --git a/app/server.R b/app/server.R index f990473..0551a64 100644 --- a/app/server.R +++ b/app/server.R @@ -1991,7 +1991,7 @@ server <- function(input, output, session) { input$linear_fit, input$color_base_scatter, input$color_comp_scatter - ) %>% + )$sc_plot %>% interactive_scatter( input$y_scatter, input$x_scatter, @@ -2418,6 +2418,62 @@ server <- function(input, output, session) { } ) + # Downloadable csv of Bivariate dataset + + observe({ + # Check the condition + + inputs_not_blank <- input$country_scatter != "" && + input$y_scatter != "" && + input$x_scatter != "" + + # Check the condition using check_data and input completeness + condition <- inputs_not_blank && + check_data(global_data, input$country_scatter, input$y_scatter, input$x_scatter) == FALSE + + # Show or hide the button based on the condition + if (condition) { + shinyjs::show("download_bivariate_data") + } else { + shinyjs::hide("download_bivariate_data") + } + }) + + + output$download_bivariate_data <- + downloadHandler( + filename = function() { + paste0("CLIAR Bivariate Analysis-",input$country_scatter," - data.csv") + }, + content = function(file) { + + show_modal_spinner( + color = "#17a2b8", + text = "Loading Data", + ) + + on.exit(remove_modal_spinner()) + + write_csv( + static_scatter( + global_data, + input$country_scatter, + input$countries_scatter, + high_group(), + input$y_scatter, + input$x_scatter, + variable_names, + country_list, + input$linear_fit, + input$color_base_scatter, + input$color_comp_scatter + )$sc_data, + file, + na = "") + } + ) + + # Report ================================================================================ @@ -2843,6 +2899,13 @@ server <- function(input, output, session) { shinyjs::show("download_data_1") }) + observeEvent(input$family, { + if (input$family == "SOE Corporate Governance" || input$family == "Labor and Social Protection Institutions" ) + shinyjs::hide("download_data_1") + else + shinyjs::show("download_data_1") + }) + ## Save inputs to be loaded the next time -------------------------------------------------------- diff --git a/app/ui.R b/app/ui.R index c44ab0e..55217a8 100644 --- a/app/ui.R +++ b/app/ui.R @@ -1013,7 +1013,17 @@ ui <- value = FALSE, status = "success" ) - ) + ), + column( + width = 6), + column( + width = 2.4, + shinyjs::hidden(downloadButton( + "download_bivariate_data", + "Download Chart Data", + style = "width:100%; background-color: #204d74; color: white" + )) + ), ) ),