根据 Shiny 应用程序中模块的输入禁用 UI 中的按钮

在 Shiny 应用中,我尝试根据用户从模块输入的内容禁用/启用主应用 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",radioButtons(inputId = ns("item3"),label = "Item 3",selected = character(0))
  )
}

mod_mod1_server <- function(id){
  moduleServer( id,function(input,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 变量未在主应用程序中更新。

fatglen 回答:根据 Shiny 应用程序中模块的输入禁用 UI 中的按钮

我不清楚您是否希望在用户仅响应 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",radioButtons(inputId = ns("item3"),label = "Item 3",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,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)
本文链接:https://www.f2er.com/374393.html

大家都在问