bill rowe
bill rowe

Reputation: 43

Viewing selected values of selectizeGroup-module parameters

I am using selectizeGroup-module and find it wonderful for interdependent filters but how can I access the filters actually selected. https://rdrr.io/cran/shinyWidgets/man/selectizeGroup-module.html has this bit of code. How can I see or pull out the values in vars_r?

vars_r <- reactive({ input$vars })

Here is the example code from the git repo. Is vars_r a vector or a list? I want to get at the values selected for each parm.

  library(shiny)
  library(shinyWidgets)

  data("mpg", package = "ggplot2")

  ui <- fluidPage(
    fluidRow(
      column(
        width = 10, offset = 1,
        tags$h3("Filter data with selectize group"),
        panel(
          checkboxGroupInput(
            inputId = "vars",
            label = "Variables to use:",
            choices = c("manufacturer", "model", "trans", "class"),
            selected = c("manufacturer", "model", "trans", "class"),
            inline = TRUE
          ),
          selectizeGroupUI(
            id = "my-filters",
            params = list(
              manufacturer = list(inputId = "manufacturer", title = "Manufacturer:"),
              model = list(inputId = "model", title = "Model:"),
              trans = list(inputId = "trans", title = "Trans:"),
              class = list(inputId = "class", title = "Class:")
            )
          ),
          status = "primary"
        ),
        DT::dataTableOutput(outputId = "table")
      )
    )
  )

  server <- function(input, output, session) {

    vars_r <- reactive({
      input$vars
    })

    res_mod <- callModule(
      module = selectizeGroupServer,
      id = "my-filters",
      data = mpg,
      vars = vars_r
    )

    output$table <- DT::renderDataTable({
      req(res_mod())
      res_mod()
    })
  }

  shinyApp(ui, server)

Upvotes: 1

Views: 467

Answers (1)

ismirsehregal
ismirsehregal

Reputation: 33510

Regarding the example you provided the input id's you are looking for are:

"my-filters-manufacturer"
"my-filters-trans"
"my-filters-model"
"my-filters-class" 

Accordingly it is a combination of your selectizeGroupUI id and the according column name.

I made an example on how to output the selection made in the selectizeInput assigned to the column manufacturer - If you make a selection in the "manufacturer-input" it will print at the bottom of the app.

Please check the following:

library(shiny)
library(shinyWidgets)

data("mpg", package = "ggplot2")

ui <- fluidPage(
  fluidRow(
    column(
      width = 10, offset = 1,
      tags$h3("Filter data with selectize group"),
      panel(
        checkboxGroupInput(
          inputId = "vars",
          label = "Variables to use:",
          choices = c("manufacturer", "model", "trans", "class"),
          selected = c("manufacturer", "model", "trans", "class"),
          inline = TRUE
        ),
        selectizeGroupUI(
          id = "my-filters",
          params = list(
            manufacturer = list(inputId = "manufacturer", title = "Manufacturer:"),
            model = list(inputId = "model", title = "Model:"),
            trans = list(inputId = "trans", title = "Trans:"),
            class = list(inputId = "class", title = "Class:")
          )
        ),
        status = "primary"
      ),
      DT::dataTableOutput(outputId = "table"),
      textOutput("manufacturer_selection")
    )
  )
)

server <- function(input, output, session) {
  
  vars_r <- reactive({
    input$vars
  })
  
  output$manufacturer_selection <- renderText({
    # print(names(input))
    # [1] "my-filters-reset_all"    "my-filters-manufacturer" "my-filters-trans"        "my-filters-model"        "my-filters-class"       
    # [6] "vars
    input[["my-filters-manufacturer"]]
  })
  
  res_mod <- callModule(
    module = selectizeGroupServer,
    id = "my-filters",
    data = mpg,
    vars = vars_r
  )
  
  output$table <- DT::renderDataTable({
    req(res_mod())
    res_mod()
  })
}

shinyApp(ui, server)

result

Upvotes: 1

Related Questions