我同意,这是一些糟糕的设计。我什至不能说,为什么addPopover
不起作用。可能是因为观察者没有一一执行命令……
但是,有一种方法可以解决您的问题。通过重写bsPopover
,我们可以考虑到相应元素的变化。
我创建了一个updateResistantPopover
函数,它向元素添加了一个额外的 eventListener (mutationListener),给出了谁的 id,每当元素的某个子元素发生变化时,它就会重新安装弹出框。
下面的示例代码:
library(shiny)
library(shinyBS)
updateResistantPopover <- function(id, title, content, placement = "bottom", trigger = "hover", options = NULL){
options = shinyBS:::buildTooltipOrPopoverOptionsList(title, placement, trigger, options, content)
options = paste0("{'", paste(names(options), options, sep = "': '", collapse = "', '"), "'}")
bsTag <- shiny::tags$script(shiny::HTML(paste0("
$(document).ready(function() {
var target = document.querySelector('#", id, "');
var observer = new MutationObserver(function(mutations) {
setTimeout(function() {
shinyBS.addTooltip('", id, "', 'popover', ", options, ");
}, 200);
});
observer.observe(target, { childList: true });
});
")))
htmltools::attachDependencies(bsTag, shinyBS:::shinyBSDep)
}
ui <- shinyUI(fluidPage(
selectInput("Main2_1","Label","abc", selectize = TRUE, multiple = TRUE),
updateResistantPopover("Main2_1", "Label", "content", placement = "right", trigger = "focus"),
actionButton("destroy", "destroy!")
))
server <- function(input, output, session){
observeEvent(input$destroy, {
updateSelectInput(session, "Main2_1", choices="foo")
})
}
shinyApp(ui, server)