diff --git a/R/buttons.R b/R/buttons.R index 50a8b33..f9a8a2b 100644 --- a/R/buttons.R +++ b/R/buttons.R @@ -22,19 +22,30 @@ downloadTable <- function(id, lab) { glue::glue() |> as.character() } -downloadPlot <- function(outputId) { +downloadPlot <- function(outputId, id) { + output <- omopViewerPlots$output[omopViewerPlots$plot_id == id] + buttons <- switch( + output, + "ggplot2" = 'shiny::numericInput(inputId = "{outputId}_width", label = "Width", value = 15), + shiny::numericInput(inputId = "{outputId}_height", label = "Height", value = 10), + {selector("{outputId}_units", "Units", {cast(c("px", "cm", "inch"))}, {cast("cm")}, FALSE)}, + shiny::numericInput(inputId = "{outputId}_dpi", label = "dpi", value = 300)' |> + glue::glue() |> + glue::glue(), + "grViz" = 'shiny::numericInput(inputId = "{outputId}_width", label = "Width (px)", value = 15), + shiny::numericInput(inputId = "{outputId}_height", label = "Height (px)", value = 10)' |> + glue::glue() |> + glue::glue() + ) + 'bslib::card_header( bslib::popover( shiny::icon("download"), - shiny::numericInput(inputId = "{outputId}_width", label = "width", value = 15), - shiny::numericInput(inputId = "{outputId}_height", label = "height", value = 10), - {selector("{outputId}_units", "Units", {cast(c("px", "cm", "inch"))}, {cast("cm")}, FALSE)}, - shiny::numericInput(inputId = "{outputId}_dpi", label = "dpi", value = 300), + {buttons}, shiny::downloadButton(outputId = "{outputId}", label = "Download png") ), class = "text-end" )' |> - glue::glue() |> glue::glue() |> as.character() } diff --git a/R/panelPlot.R b/R/panelPlot.R index 82fb940..95f8e61 100644 --- a/R/panelPlot.R +++ b/R/panelPlot.R @@ -30,7 +30,7 @@ getPlotPanel <- function(id, tab, choic) { title = "{tit}", bslib::card( full_screen = TRUE, - {downloadPlot(downloadId)}, + {downloadPlot(downloadId, id)}, bslib::layout_sidebar( sidebar = bslib::sidebar( {buttons} @@ -118,7 +118,7 @@ plotsServer <- function(rt, data) { filename = "plot_[rt].png", content = function(file) { plt <- createPlot[id]() - [savePlotFunction(id)] + [savePlotFunction(rt, id)] } )' ) |> @@ -147,11 +147,22 @@ createPlotFunction <- function(id) { result{args})" |> glue::glue() } -savePlotFunction <- function(id) { +savePlotFunction <- function(rt, id) { output <- omopViewerPlots$output[omopViewerPlots$plot_id == id] switch(output, - "ggplot2" = "ggplot2::ggsave(filename = file, plot = plt)", - "grViz" = "DiagrammeR::export_graph(graph = plt, file_name = file, fily_type = 'png', width = 800)") + "ggplot2" = "ggplot2::ggsave( + filename = file, plot = plt, + width = as.numeric(input${rt}_plot_{id}_download_width), + height = as.numeric(input${rt}_plot_{id}_download_height), + units = input${rt}_plot_{id}_download_units, + dpi = as.numeric(input${rt}_plot_{id}_download_dpi) + )" |> glue::glue(), + "grViz" = "DiagrammeR::export_graph( + graph = plt, file_name = file, fily_type = 'png', + width = as.numeric(input${rt}_plot_{id}_download_width), + height = as.numeric(input${rt}_plot_{id}_download_height) + )" |> glue::glue() + ) } renderPlotFunction <- function(id) { output <- omopViewerPlots$output[omopViewerPlots$plot_id == id] diff --git a/tests/testthat/_snaps/appStatic.md b/tests/testthat/_snaps/appStatic.md index a5573a3..8f545fc 100644 --- a/tests/testthat/_snaps/appStatic.md +++ b/tests/testthat/_snaps/appStatic.md @@ -248,8 +248,8 @@ bslib::card_header( bslib::popover( shiny::icon("download"), - shiny::numericInput(inputId = "summarise_characteristics_plot_4_download_width", label = "width", value = 15), - shiny::numericInput(inputId = "summarise_characteristics_plot_4_download_height", label = "height", value = 10), + shiny::numericInput(inputId = "summarise_characteristics_plot_4_download_width", label = "Width", value = 15), + shiny::numericInput(inputId = "summarise_characteristics_plot_4_download_height", label = "Height", value = 10), shiny::selectizeInput( inputId = "summarise_characteristics_plot_4_download_units", label = "Units", @@ -486,17 +486,8 @@ bslib::card_header( bslib::popover( shiny::icon("download"), - shiny::numericInput(inputId = "summarise_cohort_attrition_plot_2_download_width", label = "width", value = 15), - shiny::numericInput(inputId = "summarise_cohort_attrition_plot_2_download_height", label = "height", value = 10), - shiny::selectizeInput( - inputId = "summarise_cohort_attrition_plot_2_download_units", - label = "Units", - choices = c("px", "cm", "inch"), - selected = c("cm"), - multiple = FALSE, - options = list(plugins = "remove_button") - ), - shiny::numericInput(inputId = "summarise_cohort_attrition_plot_2_download_dpi", label = "dpi", value = 300), + shiny::numericInput(inputId = "summarise_cohort_attrition_plot_2_download_width", label = "Width (px)", value = 15), + shiny::numericInput(inputId = "summarise_cohort_attrition_plot_2_download_height", label = "Height (px)", value = 10), shiny::downloadButton(outputId = "summarise_cohort_attrition_plot_2_download", label = "Download png") ), class = "text-end" @@ -676,8 +667,8 @@ bslib::card_header( bslib::popover( shiny::icon("download"), - shiny::numericInput(inputId = "summarise_cohort_count_plot_5_download_width", label = "width", value = 15), - shiny::numericInput(inputId = "summarise_cohort_count_plot_5_download_height", label = "height", value = 10), + shiny::numericInput(inputId = "summarise_cohort_count_plot_5_download_width", label = "Width", value = 15), + shiny::numericInput(inputId = "summarise_cohort_count_plot_5_download_height", label = "Height", value = 10), shiny::selectizeInput( inputId = "summarise_cohort_count_plot_5_download_units", label = "Units", @@ -879,8 +870,8 @@ bslib::card_header( bslib::popover( shiny::icon("download"), - shiny::numericInput(inputId = "summarise_cohort_overlap_plot_1_download_width", label = "width", value = 15), - shiny::numericInput(inputId = "summarise_cohort_overlap_plot_1_download_height", label = "height", value = 10), + shiny::numericInput(inputId = "summarise_cohort_overlap_plot_1_download_width", label = "Width", value = 15), + shiny::numericInput(inputId = "summarise_cohort_overlap_plot_1_download_height", label = "Height", value = 10), shiny::selectizeInput( inputId = "summarise_cohort_overlap_plot_1_download_units", label = "Units", @@ -1090,8 +1081,8 @@ bslib::card_header( bslib::popover( shiny::icon("download"), - shiny::numericInput(inputId = "summarise_cohort_timing_plot_3_download_width", label = "width", value = 15), - shiny::numericInput(inputId = "summarise_cohort_timing_plot_3_download_height", label = "height", value = 10), + shiny::numericInput(inputId = "summarise_cohort_timing_plot_3_download_width", label = "Width", value = 15), + shiny::numericInput(inputId = "summarise_cohort_timing_plot_3_download_height", label = "Height", value = 10), shiny::selectizeInput( inputId = "summarise_cohort_timing_plot_3_download_units", label = "Units", @@ -1459,7 +1450,13 @@ filename = "plot_summarise_characteristics.png", content = function(file) { plt <- createPlot4() - ggplot2::ggsave(filename = file, plot = plt) + ggplot2::ggsave( + filename = file, plot = plt, + width = as.numeric(input$summarise_characteristics_plot_4_download_width), + height = as.numeric(input$summarise_characteristics_plot_4_download_height), + units = input$summarise_characteristics_plot_4_download_units, + dpi = as.numeric(input$summarise_characteristics_plot_4_download_dpi) + ) } ) @@ -1537,7 +1534,11 @@ filename = "plot_summarise_cohort_attrition.png", content = function(file) { plt <- createPlot2() - DiagrammeR::export_graph(graph = plt, file_name = file, fily_type = "png", width = 800) + DiagrammeR::export_graph( + graph = plt, file_name = file, fily_type = "png", + width = as.numeric(input$summarise_cohort_attrition_plot_2_download_width), + height = as.numeric(input$summarise_cohort_attrition_plot_2_download_height) + ) } ) @@ -1617,7 +1618,13 @@ filename = "plot_summarise_cohort_count.png", content = function(file) { plt <- createPlot5() - ggplot2::ggsave(filename = file, plot = plt) + ggplot2::ggsave( + filename = file, plot = plt, + width = as.numeric(input$summarise_cohort_count_plot_5_download_width), + height = as.numeric(input$summarise_cohort_count_plot_5_download_height), + units = input$summarise_cohort_count_plot_5_download_units, + dpi = as.numeric(input$summarise_cohort_count_plot_5_download_dpi) + ) } ) @@ -1697,7 +1704,13 @@ filename = "plot_summarise_cohort_overlap.png", content = function(file) { plt <- createPlot1() - ggplot2::ggsave(filename = file, plot = plt) + ggplot2::ggsave( + filename = file, plot = plt, + width = as.numeric(input$summarise_cohort_overlap_plot_1_download_width), + height = as.numeric(input$summarise_cohort_overlap_plot_1_download_height), + units = input$summarise_cohort_overlap_plot_1_download_units, + dpi = as.numeric(input$summarise_cohort_overlap_plot_1_download_dpi) + ) } ) @@ -1780,7 +1793,13 @@ filename = "plot_summarise_cohort_timing.png", content = function(file) { plt <- createPlot3() - ggplot2::ggsave(filename = file, plot = plt) + ggplot2::ggsave( + filename = file, plot = plt, + width = as.numeric(input$summarise_cohort_timing_plot_3_download_width), + height = as.numeric(input$summarise_cohort_timing_plot_3_download_height), + units = input$summarise_cohort_timing_plot_3_download_units, + dpi = as.numeric(input$summarise_cohort_timing_plot_3_download_dpi) + ) } )