【发布时间】:2021-03-23 08:37:03
【问题描述】:
我的应用程序应遵循以下逻辑:如果按下操作按钮,则所有输入都将被禁用并执行长时间计算。当计算完成并绘制结果时,除了操作按钮之外的所有输入都将再次启用。如果用户决定更改一个输入,则操作按钮将启用。
除了最后一点,动作按钮的启用之外,大多数这种期望的行为都可以正常工作。这是我的服务器功能(操作按钮名为“go”):
server <- function(input, output, session) {
allinputIds <- reactive(names(input))
shiny::observeEvent(input$go, {
for (id in allinputIds()) shinyjs::disable(id)
})
# ==> here is some trouble: not working
shiny::observeEvent(allinputIds(), shinyjs::enable("go"))
# from here starts the real work
bins <- shiny::eventReactive(input$go, {
x <- faithful$waiting
Sys.sleep(1.5)
seq(min(x), max(x), length.out = input$bins + 1)
})
output$figure <- shiny::renderPlot({
x <- faithful$waiting
hist(
x, breaks = bins(), col = "#75AADB", border = "white",
xlab = "Waiting time to next eruption (in mins)",
main = "Histogram of waiting times"
)
for (id in setdiff(allinputIds(), "go")) shinyjs::enable(id)
})
}
如何观察任何输入已更改?我尝试了input,而不是标记为“==>”的行之后的allinputIds(),但这都没有用。
作为第二个问题,您建议封装这个按钮/禁用/启用模式,我计划在多个闪亮模块上使用它。如果我能专注于主要代码,即bins 和output$figure <- ...,那就太好了。
感谢任何提示!
为了重现性,这里是ui函数:
ui <- shiny::tagList(
shinyjs::useShinyjs(),
shiny::navbarPage(title="Test 2",
tabPanel(title="Old Faithful",
shiny::sidebarLayout(
shiny::sidebarPanel(
shiny::sliderInput(
inputId = "bins",
label = "Number of bins:",
min = 1,
max = 50,
value = 30
)
),
shiny::mainPanel(
shiny::actionButton("go", "Update"),
shinycssloaders::withSpinner(plotOutput(outputId="figure")),
shiny::h4(shiny::textOutput("msg"))
)
)
)
)
)
shiny::shinyApp(ui, server)
【问题讨论】: