R 在闪亮的模块外使用selectInput选项

R 在闪亮的模块外使用selectInput选项,r,shiny,R,Shiny,我有两个selectInput模块,其中第二个模块的选择取决于第一个模块的选择。代码一直到这一部分,两个selectInputs对选择做出反应 我的问题稍后会出现,我想使用来自两个select输入的选择并根据它们过滤数据 完整的应用程序代码: library(shiny) library(tidyverse) #data C1 <- c('A', 'A', 'B', 'B', 'C', 'C') C2 <- c('a1', 'a2', 'b1', 'b2', 'c1', 'c2')

我有两个selectInput模块,其中第二个模块的选择取决于第一个模块的选择。代码一直到这一部分,两个selectInputs对选择做出反应

我的问题稍后会出现,我想使用来自两个select输入的选择并根据它们过滤数据

完整的应用程序代码:

library(shiny)
library(tidyverse)

#data
C1 <- c('A', 'A', 'B', 'B', 'C', 'C')
C2 <- c('a1', 'a2', 'b1', 'b2', 'c1', 'c2')
Value <- c(1,2,3,4,5,6)
data <- data.frame(C1,C2,Value)

# 1st selectinput module
mod1UI <-  function(id) {
  ns <- NS(id)
  uiOutput(ns('select1'))
}
mod1Server <- function(input, output, session) {
  output$select1 <- renderUI({
    selectInput(inputId = session$ns('select1'),
                label = 'Select Cat 1',
                choices = unique(data$C1)
    )
    
  })
  
}

# 2nd selectinput module
mod2UI <- function(id) {
  ns <- NS(id)
  uiOutput(ns('select2'))
}

mod2Server <- function(input, output, session) {
  output$select2 <- renderUI({
    selectInput(inputId = session$ns('select2'),
                label = 'Select Cat 2',
                choices = '')
  
  })
  
  observeEvent(input$select1,{
    df2 <- data %>%
      filter(C1 == input$select1) 
    
    updateSelectInput(session,
                      inputId = 'select2',
                      label = 'Select Cat 2',
                      choices = unique(df2$C2)
                      )
    
    
  })
  
}

# UI
ui <- fluidPage(
  mod1UI(id = 'select1'),
  mod2UI(id = 'select1'),
  dataTableOutput('data_flt')
)

# Server
server <- function(input, output, session) {
  
  #Call modules
  callModule(mod1Server, id = 'select1')
  callModule(mod2Server, id = 'select1')

  # Filter data
  data_filtered <- reactive({
    
    df <- data %>%
      filter(C1 == input$select1,
             C2 == input$select2)
    
    return(df)
    
  })
  
  # Print data
  output$data_flt <- renderDataTable({
    data_filtered()
  })
  
}


shinyApp(ui, server)

库(闪亮)
图书馆(tidyverse)
#资料

C1您的两个模块使用相同的
id
。这将是一个问题。让每个模块以响应的形式返回其值。例如,
在第一个模块中返回reactive({input$select1})
。然后参考模块返回值,而不是主服务器中的闪亮小部件。(即
select1()
而不是
input$select1
)。说明了您需要的技术。