Skip to content

Add option to set initial selected values in select_group_ui if supplied in params#113

Merged
pvictor merged 1 commit intodreamRs:masterfrom
PaulC91:master
Apr 1, 2025
Merged

Add option to set initial selected values in select_group_ui if supplied in params#113
pvictor merged 1 commit intodreamRs:masterfrom
PaulC91:master

Conversation

@PaulC91
Copy link
Copy Markdown
Contributor

@PaulC91 PaulC91 commented Mar 27, 2025

Sorry I'm using the new Air code formatter so most of these changes are formatting. The only meaningful change is line 62 and 63.

If we pass input$selected from the params list to both choices and selected arguments, the virtual select input will render with these initial options already selected, then the server will update the choices options with all those found in the data, maintaining the initial selection (as long as the values are found in the data). If no selected value is passed in params we get the same behavior as before with NULL being initially passed to choices and selected.

This fulfills the request in #107 and avoids having to do this from the server which has the issue of initially still returning an unfiltered data frame before inputs are updated with selected values.

Example:

library(shiny)
library(datamods)
library(shinyWidgets)


ui <- fluidPage(
  fluidRow(
    column(
      width = 10,
      offset = 1,
      tags$h3("Filter data with select group module"),
      shinyWidgets::panel(
        select_group_ui(
          id = "my-filters",
          params = list(
            list(inputId = "Manufacturer", label = "Manufacturer:", selected = "Audi"), # pass init select vals here
            list(inputId = "Type", label = "Type:", selected = "Compact"),
            list(inputId = "AirBags", label = "AirBags:"),
            list(inputId = "DriveTrain", label = "DriveTrain:")
          ),
          vs_args = list(disableSelectAll = FALSE)
        ),
        status = "primary"
      ),
      reactable::reactableOutput(outputId = "table"),
      tags$b("Inputs values:"),
      verbatimTextOutput("inputs")
    )
  )
)

server <- function(input, output, session) {
  res_mod <- select_group_server(
    id = "my-filters",
    data = reactive(MASS::Cars93),
    vars = reactive(c("Manufacturer", "Type", "AirBags", "DriveTrain"))
  )

  output$table <- reactable::renderReactable({
    reactable::reactable(res_mod())
  })

  output$inputs <- renderPrint({
    attr(res_mod(), "inputs")
  })
}

if (interactive()) shinyApp(ui, server)

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Mar 27, 2025

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

Attention: Patch coverage is 0% with 39 lines in your changes missing coverage. Please review.

Project coverage is 47.07%. Comparing base (2f37274) to head (a2cd6bb).
Report is 71 commits behind head on master.

Files with missing lines Patch % Lines
R/select-group.R 0.00% 39 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #113      +/-   ##
==========================================
- Coverage   49.35%   47.07%   -2.28%     
==========================================
  Files          20       21       +1     
  Lines        3096     4346    +1250     
==========================================
+ Hits         1528     2046     +518     
- Misses       1568     2300     +732     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pvictor pvictor merged commit cbd1ffd into dreamRs:master Apr 1, 2025
7 checks passed
@pvictor
Copy link
Copy Markdown
Member

pvictor commented Apr 1, 2025

Great! Thank you!

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

Successfully merging this pull request may close these issues.

3 participants