2

所以这是 selectizeGroupUI 和 Server 如何工作的一个很好的例子。所有过滤器都是交互式的,选择会根据其他过滤器选择进行更新。但我想访问例如值,例如:input$var_two等以在其他表达式中使用。

我正在尝试 input$my-filters使用反引号等进行访问,但一切都返回 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)
)
4

1 回答 1

3

您可以通过在brackets中使用两个inputId来访问服务器中的输入值(您需要将两个id与 - 结合起来):input[["my-filters-var_two"]]

例如,我扩展了您的代码以将所选值显示为input$var_two表格下方的文本。

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)
)
于 2021-02-07T21:40:47.140 回答