我想使用shiny-busy
将有光泽的类Shiny.onInputChange
的信息传递给服务器端。我差点用MutationObserver
:
www/
目录中的isShinyBusy.js :
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
var attributeValue = $(mutation.target).prop(mutation.attributeName);
if (mutation.attributeName === "class" && attributeValue == "shiny-busy") {
console.log("Class attribute changed to:", attributeValue);
Shiny.onInputChange("isShinyBusy", true);
} else {
Shiny.onInputChange("isShinyBusy", false);
}
});
});
$( document ).ready(function() {
observer.observe($("html")[0], {
attributes: true
});
});
app.R :
library(shiny)
shinyApp(
ui = fluidPage(
tags$head(tags$script(src = "isShinyBusy.js")),
br(),
actionButton("btn", "Click")
),
server = function(input, output) {
observeEvent(input$btn, ignoreInit = TRUE, {
Sys.sleep(5)
message("Button is pressed")
})
observe({
is.shiny.busy <- input$isShinyBusy
message("Shiny is busy: ", is.shiny.busy)
})
}
)
但是,您可以在Web浏览器控制台中看到该观察者多次触发:
并在rstudio终端:
你能解释一下为什么会这样吗?