R语言 如何将shiny inputSelect值传递给服务器模块

wlzqhblo  于 2023-06-19  发布在  其他
关注(0)|答案(1)|浏览(148)

我有一个闪亮的代码,类似于下面的人为例子。我的意图是,在server部分,我动态地将inputSelect值作为参数传递给table_Server函数,如下所示(不工作):

# Line 94 of code
server = function(input,output,session){
  
  table_Server("ER", input$region_choice)
}

相反,我必须硬编码该区域,如下所示:

# Line 94 of code
server = function(input,output,session){
  
  table_Server("ER", "Morogoro)
}

完整的运行代码(硬编码)如下,任何建议赞赏.

library(shiny)
library(shinydashboard)
#> 
#> Attaching package: 'shinydashboard'
#> The following object is masked from 'package:graphics':
#> 
#>     box

get_dataset = function(region){
  if(region=="Morogoro"){
    mtcars
  }else{
    iris
  }
}

get_reg_rate = function(region){
  data.frame(
    region="Morogoro",
    numerator=459,
    denominator=541,
    green_gap=80,
    yellow_gap=77,
    message="Regional Performance"
  )
}

table_UI <- function(id) {
  ns <- NS(id)
  tagList(
    sidebarLayout(
      sidebarPanel(width = 2,
                   uiOutput(ns("selector")),
      ),
      mainPanel(
        valueBoxOutput(ns('regional_value')),
        valueBoxOutput(ns('green_gap_value')),
        valueBoxOutput(ns('yellow_gap_value')),
        DT::dataTableOutput(ns('table'))
      )
    )
  )
}

table_Server <- function(id, region) {
  moduleServer(id,function(input, output, session) {
    
    ds=get_dataset(region)
    rate=get_reg_rate(region)
    
    output$table = DT::renderDataTable({
      ds
    })
    
    output$regional_value <- renderValueBox({
      valueBox(
        rate$rate,
        rate$message
      )
    })
    
    if(!id %in% c("DE","Score_district","DE_district")){
      output$green_gap_value <- renderValueBox({
        valueBox(
          rate$green_gap,
          "Green Gap"
        )
      })
      
      output$yellow_gap_value <- renderValueBox({
        valueBox(
          rate$yellow_gap,
          "Yellow Gap"
        )
      })
    }
    output$selector=renderUI({
      selectInput(inputId=NS(id,"region_choice"),
                  label="Region",
                  choices = c("Morogoro","Lindi","Mtwara","Njombe","Ruvuma",
                              "Iringa"),selected = "Morogoro" )
    })
  }
  )
}

ui = fluidPage(
  tabsetPanel(id = 'cqi_indicators',
              tabPanel('Region',
                       tabsetPanel(
                         id='region_indicators',
                         tabPanel("Early Retention",table_UI("ER"))
                       )
              )
              
  )
)

server = function(input,output,session){

  table_Server("ER", "Morogoro")
}

shinyApp(ui,server)
#> PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.

静态R Markdown文档中不支持Shiny应用程序
reprex package(v2.0.1)于2023-06-17创建

z0qdvdin

z0qdvdin1#

调用table_Server("ER", input$region_choice)后,input$region_choice的值还不存在->
从模块服务器函数内部访问input$region_choice
使用observeEvent使renderDataTablerenderValueBoxinput$region_choice更改时响应input$region_choice

library(shiny)
library(shinydashboard)

get_dataset = function(region){
  if(region=="Morogoro"){
    mtcars
  }else{
    iris
  }
}

get_reg_rate = function(region){
  data.frame(
    region="Morogoro",
    numerator=459,
    denominator=541,
    green_gap=80,
    yellow_gap=77,
    message="Regional Performance"
  )
}

table_UI <- function(id) {
  ns <- NS(id)
  tagList(
    sidebarLayout(
      sidebarPanel(width = 2,
                   uiOutput(ns("selector")),
      ),
      mainPanel(
        valueBoxOutput(ns('regional_value')),
        valueBoxOutput(ns('green_gap_value')),
        valueBoxOutput(ns('yellow_gap_value')),
        DT::dataTableOutput(ns('table'))
      )
    )
  )
}

table_Server <- function(id, input) {
  moduleServer(id,function(input, output, session) {
    
    observeEvent(input$region_choice,{
      ds = get_dataset(input$region_choice)
      rate = get_reg_rate(input$region_choice)
      
      output$table = DT::renderDataTable({
        ds
      })
      
      output$regional_value <- renderValueBox({
        valueBox(
          rate$rate,
          rate$message
        )
      })
      
      if(!id %in% c("DE","Score_district","DE_district")){
        output$green_gap_value <- renderValueBox({
          valueBox(
            rate$green_gap,
            "Green Gap"
          )
        })
        
        output$yellow_gap_value <- renderValueBox({
          valueBox(
            rate$yellow_gap,
            "Yellow Gap"
          )
        })
      }
    })
    
    output$selector=renderUI({
      selectInput(inputId=NS(id,"region_choice"),
                  label="Region",
                  choices = c("Morogoro","Lindi","Mtwara","Njombe","Ruvuma",
                              "Iringa"),selected = "Morogoro" )
    })
  })
}

ui = fluidPage(
  tabsetPanel(id = 'cqi_indicators',
              tabPanel('Region',
                       tabsetPanel(
                         id='region_indicators',
                         tabPanel("Early Retention",table_UI("ER"))
                       )
              )
              
  )
)

server = function(input,output,session){
  
  table_Server("ER", input)
}

shinyApp(ui,server)

相关问题