diff --git a/NAMESPACE b/NAMESPACE index a9d0ec43..63dcfa25 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -51,6 +51,8 @@ importFrom(datamods,import_modal) importFrom(datamods,import_server) importFrom(datamods,set_i18n) importFrom(datamods,show_data) +importFrom(datamods,update_variables_server) +importFrom(datamods,update_variables_ui) importFrom(ggplot2,"%+%") importFrom(ggplot2,aes) importFrom(ggplot2,coord_flip) diff --git a/R/esquisse-server.R b/R/esquisse-server.R index 2c09f88b..3f1e071f 100644 --- a/R/esquisse-server.R +++ b/R/esquisse-server.R @@ -103,22 +103,7 @@ esquisse_server <- function(id, show_data_server("show_data", reactive(controls_rv$data)) # update variable modal - observeEvent(input$update_variable, { - showModal(modalDialog( - title = tagList( - i18n("Update & select variables"), - button_close_modal() - ), - datamods::update_variables_ui(ns("update_variable"), title = NULL), - easyClose = TRUE, - size = "l", - footer = NULL - )) - }) - updated_data <-datamods::update_variables_server( - id = "update_variable", - data = reactive(data_chart$data) - ) + updated_data <- update_vars_server("update_variable", reactive(data_chart$data)) observeEvent(updated_data(), { data_chart$data <- updated_data() }) diff --git a/R/esquisse-ui.R b/R/esquisse-ui.R index f22ce57e..e73b0fcc 100644 --- a/R/esquisse-ui.R +++ b/R/esquisse-ui.R @@ -260,7 +260,7 @@ make_btn_header <- function(.list) { function(id) NULL }, update_variable = if (isTRUE(.list$update_variable)) { - btn_header(i18n("Update variable"), "brackets-angle") + update_vars_ui } else { function(id) NULL }, diff --git a/R/module-show-data.R b/R/module-show-data.R deleted file mode 100644 index c70f001b..00000000 --- a/R/module-show-data.R +++ /dev/null @@ -1,40 +0,0 @@ - -#' @importFrom shiny NS actionButton -#' @importFrom htmltools tagList -#' @importFrom phosphoricons ph -show_data_ui <- function(id) { - ns <- NS(id) - tagList( - actionButton( - inputId = ns("show_data"), - label = ph("table", height = "2em", title = i18n("Show data")), - class = "btn-sm btn-primary", - title = i18n("Show data") - ) - ) -} - -#' @importFrom shiny moduleServer observeEvent showNotification reactive -#' @importFrom datamods show_data -show_data_server <- function(id, data_r = reactive(NULL)) { - moduleServer( - id, - function(input, output, session) { - - observeEvent(input$show_data, { - data <- data_r() - if (!is.data.frame(data)) { - showNotification( - ui = "No data to display", - duration = 700, - id = paste("esquisse", sample.int(1e6, 1), sep = "-"), - type = "warning" - ) - } else { - show_data(data, title = i18n("Dataset"), type = "modal") - } - }) - - } - ) -} diff --git a/R/module-utils.R b/R/module-utils.R new file mode 100644 index 00000000..b2346618 --- /dev/null +++ b/R/module-utils.R @@ -0,0 +1,72 @@ + +# Show data ------------------------------------------------------------------------- + +#' @importFrom shiny NS +show_data_ui <- function(id) { + ns <- NS(id) + btn_header(i18n("Show data"), "table")(ns("btn")) +} + +#' @importFrom shiny moduleServer observeEvent showNotification reactive +#' @importFrom datamods show_data +show_data_server <- function(id, data_r = reactive(NULL)) { + moduleServer( + id, + function(input, output, session) { + + observeEvent(input$btn, { + data <- data_r() + if (!is.data.frame(data)) { + showNotification( + ui = "No data to display", + duration = 700, + id = paste("esquisse", sample.int(1e6, 1), sep = "-"), + type = "warning" + ) + } else { + show_data(data, title = i18n("Dataset"), type = "modal") + } + }) + + } + ) +} + + + + +# Update vars ----------------------------------------------------------------------- + +#' @importFrom shiny NS +update_vars_ui <- function(id) { + ns <- NS(id) + btn_header(i18n("Update variables"), "brackets-angle")(ns("btn")) +} + +#' @importFrom shiny moduleServer observeEvent modalDialog showModal reactive +#' @importFrom datamods update_variables_ui update_variables_server +update_vars_server <- function(id, data_r = reactive(NULL)) { + moduleServer( + id, + function(input, output, session) { + ns <- session$ns + observeEvent(input$btn, { + showModal(modalDialog( + title = tagList( + i18n("Update & select variables"), + button_close_modal() + ), + datamods::update_variables_ui(ns("update_variable"), title = NULL), + easyClose = TRUE, + size = "l", + footer = NULL + )) + }) + updated_data <-datamods::update_variables_server( + id = "update_variable", + data = data_r + ) + return(updated_data) + } + ) +}