Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Provide all drop down style pickers (pickerInput, virtualSelectInput) the option to update on close #677

Closed
dsen6644 opened this issue Mar 14, 2024 · 4 comments

Comments

@dsen6644
Copy link

While this has been requested before (#392), the solution for explicitly calling the _open input value adds much complexity when working in a modular workflow.

Below I have a sample app that chains reactivity between modules so that updates in the pickers higher up in the hierarchy will dynamically limit the number of options available in subsequent pickers.

Because the inputs are being generated through namespaces it becomes difficult to 'explicitly' call the _open id.

library(shiny)
library(dplyr)
library(shinyWidgets)

# module UI

moduleUI <- function(id, label, choices = NULL) {
  ns <- NS(id)
  tagList(virtualSelectInput(ns("select"), label= label, choices = choices,
                             selected = choices, multiple = TRUE))
}

# module server

moduleController <- function(id, data, selector, input_val, output_val) {
  moduleServer(id, function(input, output, session) { 
    ns <- session$ns
    observeEvent(selector(), {
      choices=data %>%
        filter({{input_val}} %in% selector()) %>%
        distinct({{output_val}}) %>%
        arrange({{output_val}}) %>%
        pull({{output_val}})
      updateVirtualSelect("select", choices = choices, selected = choices)
    }, ignoreNULL = FALSE)
    return(reactive({input$select}))
  })
}

ui_heirarchy <- function(id){
  ns <- NS(id)
  tagList(
    virtualSelectInput(ns("Module0"), label = "Region", choices = sort(unique(state.region)), selected = sort(unique(state.region)), multiple = TRUE),
    moduleUI(ns("Module1"), label = "Division"),
    moduleUI(ns("Module2"), label = "State"),
  )
}

server_heirarchy <- function(id, data) {
  moduleServer(id, function(input, output, session) {
    mod0 <- reactive({input$Module0})
    mod1 <- moduleController("Module1", data, reactive({mod0()}), region, division)
    mod2 <- moduleController("Module2", data, reactive({mod1()}), division, state)
    return(list(mod0 = mod0, mod1 = mod1, mod2 = mod2))
  })
}

# ui / server / app

ui <- fixedPage(
  ui_heirarchy("heirarchy"),
  textOutput("row_n")
)

server <- function(input, output, session) {
  state_info <- data.frame(region = state.region, division = state.division, state = state.name)
  out <- server_heirarchy("heirarchy", state_info)
  y <- reactive({
    req(out$mod2())
    state_info %>%
      filter(region %in% out$mod0()) %>%
      filter(division %in% out$mod1()) %>%
      filter(state %in% out$mod2())
  }) %>%
    debounce(500)
  output$row_n <- renderText({
    as.character(nrow(y()))})
}

shinyApp(ui, server)
@pvictor
Copy link
Member

pvictor commented Mar 19, 2024

Hi,

the solution for explicitly calling the _open input value adds much complexity when working in a modular workflow.

I'm not sure to understand, in a module you can use input$select_open like you use input$select, why do you need this here ?

I 'll take a look if it's possible to send input vlaue after close rather than on change.

@pvictor
Copy link
Member

pvictor commented Mar 20, 2024

virtualSelectInput() now has an updateOn = "change" or "close" argument.

@dsen6644
Copy link
Author

Works perfectly, thank you!!

@dsen6644
Copy link
Author

Complete

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants