在disabled按钮上添加shinyBS popover

时间:2017-07-06 10:55:35

标签: r shiny shinybs

我还没有在shinyBS和google / SO的文档中找到有关如何使用trigger = 'manual'的任何信息,例如addPopover shinyBS }。我认为这是将工具提示添加到禁用按钮的方法。 (我不希望通过div' ving按钮并将title提供给div。 如果某人有办法将工具提示反应性地添加到shiny个应用程序

,那也会很好

2 个答案:

答案 0 :(得分:2)

如果你想在弹出框中使用trigger = manual,那么你需要定义一个脚本来切换弹出框,例如用jQuery:

library(shiny)
library(shinyjs)
library(shinyBS)


ui <-shinyUI(fluidPage(useShinyjs(),
                       # press this button to trigger the popover
                       actionButton("addPopover", "Add Popover"),

                       # a disabled button
                       disabled(actionButton("disabledButton", "This button is disabled")),

                       # the popover to appear over the disabled button
                       bsPopover("disabledButton", "Popover", "Some text", trigger="manual"),

                       # the script to trigger the popover
                       uiOutput("trigger")))


server <- shinyServer(function(input,output, session){

  # on checkbox selection, disable button and trigger the popover
  output$trigger <- renderUI({
    input$addPopover
    tags$script("$('#disabledButton').popover('toggle');")
  })
})

shinyApp(ui,server)

enter image description here

如果你想在没有jQuery的情况下这样做,这将是一个解决方案:

library(shiny)
library(shinyjs)
library(shinyBS)


ui <-shinyUI(fluidPage(useShinyjs(),
                       # checkbox for disabling and tooltip action
                       checkboxInput("disable", "Disable"),

                       # a button
                       actionButton("buttonId", "Some button"),

                       # uiOutput to realize logic for tooltip
                       uiOutput("trigger")))


server <- shinyServer(function(input,output, session){

  # on click of input$addPopover, trigger the popover over the disabled button
  output$trigger <- renderUI({
    if(input$disable) { 
      shinyjs::disable("buttonId")
      bsTooltip("buttonId", "This button is currently disabled.")
    }else{
      shinyjs::enable("buttonId")
      bsTooltip("buttonId", "")
    }
  })
})

shinyApp(ui,server)

enter image description here

答案 1 :(得分:0)

由于shosaco的解决方案对我不起作用,我让它以这种方式工作:

if (input$disable) { 
  addCssClass("buttonId", "disabled")
  bsTooltip("buttonId", "This button is currently disabled.")
} else {
  bsTooltip("buttonId", "")
  removeCssClass("buttonId", "disabled")
}
observeEvent(input$buttonId, {
    if (!input$disable) {
      output$text <- renderText("Bla")
    } else {
      output$text <- renderText(NULL)
    }