简体   繁体   中英

Problem with acccesing single input in selectizeGroupUI from shinyWidgets

So this is great example of how selectizeGroupUI and Server works. All of the filters are interactive and choices are update based on other filters selections. But I would like to access for example value like: input$var_two and etc. to use in other expressions.

I was trying accesing input$my-filters using backticksand etc. but everything returns NULL


library(shiny)
library(shinyWidgets)

a_df <- tibble(
    var_one = c("hadley", "charlotte", "rené", "raymond"),
    var_two = c("mutate", "filter", "slice", "spread"),
    var_three = c("an apple", "a pipe", "a cocktail", "a dog"),
    var_four = c("with", "without", "thanks", "out of"),
    var_five = c("tidyr", "magrittr", "purrr", "dplyr")
)

ex_df <- expand.grid(a_df) # create a df with the 64 combinaisons

tib <- as_tibble(sample_n(ex_df, 40))


shinyApp(
    ui = pageWithSidebar(
        headerPanel("Painting 8"),
        sidebarPanel(
            selectizeGroupUI(
                id = "my-filters",
                inline = FALSE,
                params = list(
                    var_one = list(inputId = "var_one", title = "Select variable 1", placeholder = 'select'),
                    var_two = list(inputId = "var_two", title = "Select variable 2", placeholder = 'select'),
                    var_three = list(inputId = "var_three", title = "Select variable 3", placeholder = 'select'),
                    var_four = list(inputId = "var_four", title = "Select variable 4", placeholder = 'select'),
                    var_five = list(inputId = "var_five", title = "Select variable 5", placeholder = 'select')
                )
            )
        ),
        
        mainPanel(
            tableOutput("table")
        )
    ),
    
    server = function(input, output, session) {
        
        res_mod <- callModule(
            module = selectizeGroupServer,
            id = "my-filters",
            data = tib,
            vars = c("var_one", "var_two", "var_three", "var_four", "var_five")
        )
        
        output$table <- renderTable({
            res_mod()
        })
        
    },
    
    options = list(height = 500)
)

You can acccess the input value in the server by using both inputIds in brakets (you need to combine both ids with an -): input[["my-filters-var_two"]]

For example I expanded your code to show the selected value for input$var_two as a text underneath the table.

library(shiny)
library(shinyWidgets)

a_df <- tibble(
  var_one = c("hadley", "charlotte", "rené", "raymond"),
  var_two = c("mutate", "filter", "slice", "spread"),
  var_three = c("an apple", "a pipe", "a cocktail", "a dog"),
  var_four = c("with", "without", "thanks", "out of"),
  var_five = c("tidyr", "magrittr", "purrr", "dplyr")
)

ex_df <- expand.grid(a_df) # create a df with the 64 combinaisons

tib <- as_tibble(sample_n(ex_df, 40))


shinyApp(
  ui = pageWithSidebar(
    headerPanel("Painting 8"),
    sidebarPanel(
      selectizeGroupUI(
        id = "my-filters",
        inline = FALSE,
        params = list(
          var_one = list(inputId = "var_one", title = "Select variable 1", placeholder = 'select'),
          var_two = list(inputId = "var_two", title = "Select variable 2", placeholder = 'select'),
          var_three = list(inputId = "var_three", title = "Select variable 3", placeholder = 'select'),
          var_four = list(inputId = "var_four", title = "Select variable 4", placeholder = 'select'),
          var_five = list(inputId = "var_five", title = "Select variable 5", placeholder = 'select')
        )
      )
    ),
    
    mainPanel(
      tableOutput("table"),
      
      # SHOW SELECTED VALUE FROM var_two
      textOutput("text_var_two")
    )
  ),
  
  server = function(input, output, session) {
    
    res_mod <- callModule(
      module = selectizeGroupServer,
      id = "my-filters",
      data = tib,
      vars = c("var_one", "var_two", "var_three", "var_four", "var_five")
    )
    
    output$table <- renderTable({
      res_mod()
    })
    
    # SELECT VALUE FROM var_two
    output$text_var_two <- renderText({
      input[["my-filters-var_two"]]
      
    })
    
  },
  
  options = list(height = 500)
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM