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 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..4ae5eda --- /dev/null +++ b/inst/examples-shiny/03_repeat_names/app.R @@ -0,0 +1,116 @@ +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"), + bslib::layout_columns( + div( + span( + selectInput("column", "Select Column", head(names(iris), -1L)), + actionButton("create", "Create new filter") + ), + div(id = "filters") + ), + div( + h2("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) { + iris_columns <- head(names(iris), -1L) + session$userData$col_choices <- reactiveVal(iris_columns) + 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()) + 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({ + 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) 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}