【问题标题】:Disable button in UI based on input from module in Shiny app根据 Shiny 应用程序中模块的输入禁用 UI 中的按钮
【发布时间】:2021-08-12 02:02:44
【问题描述】:

在一个闪亮的应用程序中,我试图根据用户来自模块的输入来禁用/启用主应用程序 UI 中的操作按钮。基本上,我希望禁用“下一页”(submit)按钮,直到用户响应最后一项(item3)。当用户响应最后一项时,我希望启用该按钮。但是,我的应用没有更新操作按钮的切换状态。

这是一个使用{Golem} 结构的最小可重现示例:

app_ui.R:

library("shiny")
library("shinyjs")

app_ui <- function(request) {
  tagList(
    useShinyjs(),
    fluidPage(
      mod_mod1_ui("mod1_ui_1"),
      actionButton(inputId = "submit",
                   label = "Next Page")
    )
  )
}

app_server.R:

library("shiny")
library("shinyjs")

app_server <- function( input, output, session ) {
  state <- mod_mod1_server("mod1_ui_1")
  
  # Enable the "Next Page" button when the user responds to the last item
  observe({
    toggleState("submit", state == TRUE)
    })  
}

mod_mod1.R:

library("shiny")
library("shinyjs")

mod_mod1_ui <- function(id){
  ns <- NS(id)
  tagList(
    radioButtons(inputId = ns("item1"),
                 label = "Item 1",
                 choices = c(1, 2, 3, 4),
                 selected = character(0)),
    
    radioButtons(inputId = ns("item2"),
                 label = "Item 2",
                 choices = c(1, 2, 3, 4),
                 selected = character(0)),
    
    radioButtons(inputId = ns("item3"),
                 label = "Item 3",
                 choices = c(1, 2, 3, 4),
                 selected = character(0))
  )
}

mod_mod1_server <- function(id){
  moduleServer( id, function(input, output, session){
    ns <- session$ns
    
    # When the user completes the last survey question
    completed <- logical(1)
    
    observe({
      lastQuestion <- input$item3
      if(!is.null(lastQuestion)){
        completed <- TRUE
      } else {
        completed <- FALSE
      }
      browser()
    })
    
    return(completed)
 
  })
}

使用browser() 语句,completed 变量似乎已在模块中正确更新,但 state 变量未在主应用程序中更新。

【问题讨论】:

    标签: r shiny module golem


    【解决方案1】:

    我不清楚当用户仅响应 item3 或所有项目(1 到 3)时,您是否希望它起作用。我假设是后者。但是,您可以根据用例的需要进行修改。定义 reactiveValues 对象有效。试试这个

    library("shiny")
    library("js")
    
    mod_mod1_ui <- function(id){
      ns <- NS(id)
      tagList(
        radioButtons(inputId = ns("item1"),
                     label = "Item 1",
                     choices = c(1, 2, 3, 4),
                     selected = character(0)),
        
        radioButtons(inputId = ns("item2"),
                     label = "Item 2",
                     choices = c(1, 2, 3, 4),
                     selected = character(0)),
        
        radioButtons(inputId = ns("item3"),
                     label = "Item 3",
                     choices = c(1, 2, 3, 4),
                     selected = character(0))
      )
    }
    
    mod_mod1_server <- function(id){
      moduleServer( id, function(input, output, session){
        ns <- session$ns
        
        # When the last survey question is completed
        rv <- reactiveValues(completed=1)
        
        observe({
          lastQuestion <- input$item3
          if(!is.null(lastQuestion) & !is.null(input$item2) & !is.null(input$item1)){
            rv$completed <- 1
          } else {
            rv$completed <- 0
          }
          print(rv$completed )
          #browser()
          
        })
        
        return(rv)
      })
    }
    
    app_ui <- function(request) {
      fluidPage(
        useShinyjs(),
        tagList(
          mod_mod1_ui("mod1_ui_1"),
          actionButton(inputId = "submit",
                       label = "Next Page") 
        )
      )
    }
    
    app_server <- function( input, output, session ) {
      state <- mod_mod1_server("mod1_ui_1")
      
      # Don't show "Next Page" button until last item is completed
      observe({
        toggleState("submit", state$completed == TRUE)
      })
     
    }
    
    shinyApp(ui = app_ui, server = app_server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-04
      • 2021-04-04
      • 2021-02-28
      • 2020-11-26
      • 1970-01-01
      • 1970-01-01
      • 2019-11-08
      • 1970-01-01
      相关资源
      最近更新 更多