在 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
变量未在主应用程序中更新。