根据 Shiny 应用中模块的输入禁用 UI 中的按钮
Disable button in UI based on input from module in Shiny app
在 Shiny 应用程序中,我正在尝试根据用户从模块输入的内容 disable/enable 主应用程序 UI 中的操作按钮。基本上,我希望在用户响应最后一项 (item3
) 之前禁用“下一页”(submit
) 按钮。当用户响应最后一项时,我希望启用该按钮。但是,我的应用没有更新操作按钮的切换状态。
这是一个使用 {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
变量未在主应用程序中更新。
我不清楚当用户仅响应项目 3 或所有项目(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)
在 Shiny 应用程序中,我正在尝试根据用户从模块输入的内容 disable/enable 主应用程序 UI 中的操作按钮。基本上,我希望在用户响应最后一项 (item3
) 之前禁用“下一页”(submit
) 按钮。当用户响应最后一项时,我希望启用该按钮。但是,我的应用没有更新操作按钮的切换状态。
这是一个使用 {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
变量未在主应用程序中更新。
我不清楚当用户仅响应项目 3 或所有项目(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)