From f6925145a1e43a094b2d0a28d53152f27bf26c68 Mon Sep 17 00:00:00 2001 From: Ashley Baldry Date: Mon, 5 May 2025 11:56:59 +0100 Subject: [PATCH 1/4] Add example that reuses inputs --- inst/examples-shiny/03_repeat_names/app.R | 90 +++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 inst/examples-shiny/03_repeat_names/app.R diff --git a/inst/examples-shiny/03_repeat_names/app.R b/inst/examples-shiny/03_repeat_names/app.R new file mode 100644 index 0000000..7a935ea --- /dev/null +++ b/inst/examples-shiny/03_repeat_names/app.R @@ -0,0 +1,90 @@ +library(bslib) +library(shiny) +library(shiny.destroy) + +numericModuleUI <- function(id) { + ns <- NS(id) + + bslib::card( + id = ns("card"), + h3(id), + sliderInput( + ns("slider"), + label = "Range", + min = min(iris[[id]]), + max = max(iris[[id]]), + value = range(iris[[id]]), + step = 0.1 + ), + actionButton(ns("destroy"), "Remove card") + ) +} + +numericModuleServer <- function(id) { + moduleServer(id, function(input, output, session) { + observeEvent(input$slider, ignoreInit = TRUE, { + session$userData$filters[[id]] <- input$slider + }) + + observeEvent(input$destroy, { + destroyModule() + session$userData$filters[[id]] <- NULL + session$userData$col_choices( + intersect(names(iris), c(id, session$userData$col_choices())) + ) + }) + }) +} + +destroyableNumericModuleUI <- makeModuleUIDestroyable(numericModuleUI) +destroyableNumericModuleServer <- makeModuleServerDestroyable(numericModuleServer) + +ui <- bslib::page_fluid( + h1("Destroyable Filters"), + span( + selectInput("column", "Select Column", head(names(iris), -1L)), + actionButton("create", "Create new filter") + ), + bslib::layout_columns( + div(id = "filters"), + div( + h2("Iris"), + DT::DTOutput("iris") + ), + col_widths = c(4, 8) + ) +) + +server <- function(input, output, session) { + session$userData$col_choices <- reactiveVal(head(names(iris), -1L)) + session$userData$filters <- reactiveValues() + + observeEvent(input$create, { + insertUI("#filters", "beforeEnd", destroyableNumericModuleUI(id = input$column)) + destroyableNumericModuleServer(id = input$column) + session$userData$col_choices(setdiff(session$userData$col_choices(), input$column)) + }) + + observeEvent(session$userData$col_choices(), { + updateSelectInput(inputId = "column", choices = session$userData$col_choices()) + }) + + iris_filtered <- reactive({ + iris_x <- iris + + for (col_name in names(session$userData$filters)) { + filter_vals <- session$userData$filters[[col_name]] + if (length(filter_vals) == 2L) { + iris_x <- iris_x[ + iris_x[[col_name]] >= filter_vals[1L] & iris_x[[col_name]] <= filter_vals[2L], + ] + } + } + + iris_x + }) + + output$iris <- DT::renderDT(iris_filtered()) +} + +shinyApp(ui, server) From 8630d7ffed80147635c2d0bed904473287ed8c30 Mon Sep 17 00:00:00 2001 From: Ashley Baldry Date: Mon, 26 May 2025 10:12:43 +0100 Subject: [PATCH 2/4] Update input server side to check for console messages --- inst/examples-shiny/03_repeat_names/app.R | 40 +++++++++++++++++++---- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/inst/examples-shiny/03_repeat_names/app.R b/inst/examples-shiny/03_repeat_names/app.R index 7a935ea..4ae5eda 100644 --- a/inst/examples-shiny/03_repeat_names/app.R +++ b/inst/examples-shiny/03_repeat_names/app.R @@ -41,22 +41,32 @@ destroyableNumericModuleServer <- makeModuleServerDestroyable(numericModuleServe ui <- bslib::page_fluid( h1("Destroyable Filters"), - span( - selectInput("column", "Select Column", head(names(iris), -1L)), - actionButton("create", "Create new filter") - ), bslib::layout_columns( - div(id = "filters"), + div( + span( + selectInput("column", "Select Column", head(names(iris), -1L)), + actionButton("create", "Create new filter") + ), + div(id = "filters") + ), div( h2("Iris"), - DT::DTOutput("iris") + DT::DTOutput("iris"), + h3(class = "pt-3", "Update Filter"), + div( + class = "d-flex justify-content-around align-items-center", + selectInput("column_filter", "Select Column", NULL), + numericInput("value", "Maximum Value", value = NULL, step = 0.1), + actionButton("update", "Update Filter") + ) ), col_widths = c(4, 8) ) ) server <- function(input, output, session) { - session$userData$col_choices <- reactiveVal(head(names(iris), -1L)) + iris_columns <- head(names(iris), -1L) + session$userData$col_choices <- reactiveVal(iris_columns) session$userData$filters <- reactiveValues() observeEvent(input$create, { @@ -67,6 +77,22 @@ server <- function(input, output, session) { observeEvent(session$userData$col_choices(), { updateSelectInput(inputId = "column", choices = session$userData$col_choices()) + updateSelectInput( + inputId = "column_filter", + choices = setdiff(iris_columns, session$userData$col_choices()) + ) + }) + + observeEvent(input$update, { + req(input$column_filter) + input_id <- paste0(input$column_filter, "-slider") + req(input$value >= input[[input_id]][1]) + + updateSliderInput( + session = session, + input = input_id, + value = c(input[[input_id]][1], input$value) + ) }) iris_filtered <- reactive({ From 742eb9598a56bfd3637cf41961f776e0643f59d6 Mon Sep 17 00:00:00 2001 From: Ashley Baldry Date: Sat, 5 Jul 2025 09:41:56 +0100 Subject: [PATCH 3/4] Update docs --- man/shiny.destroy-package.Rd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/man/shiny.destroy-package.Rd b/man/shiny.destroy-package.Rd index 8a93e8c..37f2ae3 100644 --- a/man/shiny.destroy-package.Rd +++ b/man/shiny.destroy-package.Rd @@ -6,7 +6,7 @@ \alias{shiny.destroy-package} \title{shiny.destroy: Create Destroyable Modules in 'Shiny'} \description{ -This package enables the complete removal of various 'Shiny' components, such as inputs, outputs and modules. It also aids in the removal of observers that have been created in dynamically created modules. +Enables the complete removal of various 'Shiny' components, such as inputs, outputs and modules. It also aids in the removal of observers that have been created in dynamically created modules. } \author{ \strong{Maintainer}: Ashley Baldry \email{arbaldry91@gmail.com} From bdc85f50538ce16bdae36ee45e82d55db9d42226 Mon Sep 17 00:00:00 2001 From: Ashley Baldry Date: Sat, 5 Jul 2025 10:32:56 +0100 Subject: [PATCH 4/4] Update NEWS.md --- NEWS.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/NEWS.md b/NEWS.md index 61a8a23..0e10153 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ -# shiny.destroy +#shiny.destroy 0.1.1 -## 0.1.0 +- Added example to show repeat HTML IDs being used (#18) -Initial release of package +# shiny.destroy 0.1.0 + +- Initial release of package