初步答案:为单选按钮创建工具提示
迟到的答案,但在这里:
如您所见,shinyBS 的工具提示功能仅设计用于按 id 选择。你想要比这更精细的东西,所以我们需要构造一些新函数来替换更粗糙的bsTooltip
.
新函数被调用radioTooltip
,基本上是从bsTooltip
. 它还需要一个争论,即您希望将工具提示分配给的那个choice
。radioButton
这允许更精细的选择。现在不同的是在文档上选择元素的方式。无需过多介绍 JavaScript 细节,我们选择具有给定 Id 的元素并保存提供radioButton
choice
的(内部的,因此您将获得的值input$radioButtonId
)。
代码如下。我建议你试试看。
library(shiny)
library(shinyBS)
radioTooltip <- function(id, choice, title, placement = "bottom", trigger = "hover", options = NULL){
options = shinyBS:::buildTooltipOrPopoverOptionsList(title, placement, trigger, options)
options = paste0("{'", paste(names(options), options, sep = "': '", collapse = "', '"), "'}")
bsTag <- shiny::tags$script(shiny::HTML(paste0("
$(document).ready(function() {
setTimeout(function() {
$('input', $('#", id, "')).each(function(){
if(this.getAttribute('value') == '", choice, "') {
opts = $.extend(", options, ", {html: true});
$(this.parentElement).tooltip('destroy');
$(this.parentElement).tooltip(opts);
}
})
}, 500)
});
")))
htmltools::attachDependencies(bsTag, shinyBS:::shinyBSDep)
}
ui <- shinyUI(
fluidPage(
fluidRow(
column(3,
radioButtons("radioSelection", label = "So many options!", choices = c("A", "B", "C"))
),
radioTooltip(id = "radioSelection", choice = "A", title = "Button 1 Explanation", placement = "right", trigger = "hover"),
radioTooltip(id = "radioSelection", choice = "B", title = "Button 2 Explanation", placement = "right", trigger = "hover"),
radioTooltip(id = "radioSelection", choice = "C", title = "Button 3 Explanation", placement = "right", trigger = "hover"),
column(9,'Plot')
)
)
)
server <- function(input, output, session) {}
shinyApp(ui = ui, server = server)
玩得开心!
编辑:为 selectInput/selectizeInput 创建工具提示
因为selectInput
一个人不能只是改变一点,但必须有一个全新的功能。主要有一个原因。虽然radioButtons
他们的所有选择都清晰可见并且就在那里,但selectizeInput
移动选择,重新渲染它们,仅在它们第一次显示时渲染它们等等。很多事情都会发生。这就是为什么这个解决方案会抓住周围环境div
并不断倾听childNodes
被添加的原因。其余的只是(希望是有效的)过滤。
下面的示例代码:
library(shiny)
library(shinyBS)
selectizeTooltip <- function(id, choice, title, placement = "bottom", trigger = "hover", options = NULL){
options = shinyBS:::buildTooltipOrPopoverOptionsList(title, placement, trigger, options)
options = paste0("{'", paste(names(options), options, sep = "': '", collapse = "', '"), "'}")
bsTag <- shiny::tags$script(shiny::HTML(paste0("
$(document).ready(function() {
var opts = $.extend(", options, ", {html: true});
var selectizeParent = document.getElementById('", id, "').parentElement;
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation){
$(mutation.addedNodes).filter('div').filter(function(){return(this.getAttribute('data-value') == '", choice, "');}).each(function() {
$(this).tooltip('destroy');
$(this).tooltip(opts);
});
});
});
observer.observe(selectizeParent, { subtree: true, childList: true });
});
")))
htmltools::attachDependencies(bsTag, shinyBS:::shinyBSDep)
}
ui <- shinyUI(
fluidPage(
actionButton("but", "Change choices!"),
selectizeInput(inputId = "lala", label = "Label!", choices = LETTERS),
selectizeTooltip(id = "lala", choice = "c", title = "Tooltip for c", placement = "right"),
selectizeTooltip(id = "lala", choice = "C", title = "Tooltip for C", placement = "right"),
selectizeTooltip(id = "lala", choice = "F", title = "Tooltip for F", placement = "right")
)
)
server <- function(input, output, session){
observeEvent(input$but, {
updateSelectizeInput(session, "lala", choices = c("C", letters))
})
}
shinyApp(ui, server)
请注意,工具提示也updateSelectizeInput
可以保留,并且可能存在用于最初不存在的选项的工具提示。
如果人们有兴趣,我可以向shinyBS 的人发送功能请求,以便可能将其包含到他们的工作中。