0

我正在尝试访问在模块中定义的一些用户输入。在下面的代码中,我有三个pickerInput级联的选择并依赖于前一个。最终pickerInput(Level3)然后将过滤mpg数据集并将其显示在三个pickerInput项目下方。

我不确定如何访问 Level3 选择并在服务器的另一部分使用它,在这种情况下将其用作mpg.

下面是代码:

library(shiny)
library(tidyverse)
library(shinyWidgets)

multiFilterUI <- function(id){
    ns <- NS(id)
    
    tagList(
        fluidRow(
            column(3, uiOutput(ns("level1"))),
            column(3, uiOutput(ns("level2"))),
            column(3, uiOutput(ns("level3")))
        )
    )
}

multiFilterServer <- function(id, data, col1, col2, col3){
    moduleServer(
        id,
        function(input, output, session){
            output$level1 <- renderUI({
                ns <- session$ns
                cs <- unique(data[[col1]])
                
                pickerInput(ns("level1_select"), label = "Level1", choices = cs)
            })
            
            output$level2 <- renderUI({
                ns <- session$ns
                cs <- data %>% filter(!!sym(col1) %in% input$level1_select) %>%
                    distinct(!!sym(col2)) %>% 
                    pull(!!sym(col2))

                pickerInput(ns("level2_select"), label = "Level2", choices = cs)
            })
            
            output$level3 <- renderUI({
                ns <- session$ns
                cs <- data %>% filter(!!sym(col2) %in% input$level2_select) %>%
                    distinct(!!sym(col3)) %>% 
                    pull(!!sym(col3))
                
                pickerInput(ns("level3_select"), label = "Level3", choices = cs)
            })
        }
    )
}


ui <- fluidPage(
    multiFilterUI("test"),
    dataTableOutput("dt")
)

server <- function(input, output, session) {
    
    multiFilterServer("test", mpg, "manufacturer", "model", "trans")
    
    output$dt <- renderDataTable({
        mpg %>% filter(trans %in% input$level3_select)
    })
    
    
}

shinyApp(ui, server)

我的尝试只是访问level3_select,但它显然不起作用。

4

1 回答 1

1

您需要从模块作为响应返回level3_select,您可以这样做:

return(list(
        level3_select = reactive({input$level3_select})
        ))

然后在主server函数中,您可以将模块的返回值存储在变量中。当您访问反应值时,您需要通过添加来评估它()

library(shiny)
library(tidyverse)
library(shinyWidgets)

multiFilterUI <- function(id){
  ns <- NS(id)
  
  tagList(
    fluidRow(
      column(3, uiOutput(ns("level1"))),
      column(3, uiOutput(ns("level2"))),
      column(3, uiOutput(ns("level3")))
    )
  )
}

multiFilterServer <- function(id, data, col1, col2, col3){
  moduleServer(
    id,
    function(input, output, session){
      output$level1 <- renderUI({
        ns <- session$ns
        cs <- unique(data[[col1]])
        
        pickerInput(ns("level1_select"), label = "Level1", choices = cs)
      })
      
      output$level2 <- renderUI({
        ns <- session$ns
        cs <- data %>% filter(!!sym(col1) %in% input$level1_select) %>%
          distinct(!!sym(col2)) %>% 
          pull(!!sym(col2))
        
        pickerInput(ns("level2_select"), label = "Level2", choices = cs)
      })
      
      output$level3 <- renderUI({
        ns <- session$ns
        cs <- data %>% filter(!!sym(col2) %in% input$level2_select) %>%
          distinct(!!sym(col3)) %>% 
          pull(!!sym(col3))
        
        pickerInput(ns("level3_select"), label = "Level3", choices = cs)
      })
      
      return(list(
        level3_select = reactive({input$level3_select})
        ))
    }
  )
}


ui <- fluidPage(
  multiFilterUI("test"),
  dataTableOutput("dt")
)

server <- function(input, output, session) {
  
  selection <- multiFilterServer("test", mpg, "manufacturer", "model", "trans")
  
  output$dt <- renderDataTable({
    mpg %>% filter(trans %in% selection$level3_select())
  })
  
  
}

shinyApp(ui, server)
于 2020-08-07T21:10:34.913 回答