Tooltip in shiny UI for help text

前端 未结 2 2011
离开以前
离开以前 2021-01-04 22:20

I want to place a help text for check-box label as a tooltip. In the following example I use the shinyBS package - but I only get it to work for the title of th

2条回答
  •  北荒
    北荒 (楼主)
    2021-01-04 23:04

    Here is slight change - to add tooltips only to the checkboxes.

    library(shiny)
    library(shinyBS)
    
    server <- function(input, output) {
    
    makeCheckboxTooltip <- function(checkboxValue, buttonLabel, buttonId, Tooltip){
    tags$script(HTML(paste0("
                            $(document).ready(function() {
                              var inputElements = document.getElementsByTagName('input');
                              for(var i = 0; i < inputElements.length; i++) {
    
                                var input = inputElements[i];
                                if(input.getAttribute('value') == '", checkboxValue, "' && input.getAttribute('value') != 'null') {
    
                                  var button = document.createElement('button');
                                  button.setAttribute('id', '", buttonId, "');
                                  button.setAttribute('type', 'button');
                                  button.setAttribute('class', 'btn action-button btn-inverse btn-xs');
                                  button.style.float = 'right';
                                  button.appendChild(document.createTextNode('", buttonLabel, "'));
    
                                  input.parentElement.parentElement.appendChild(button);
                                  shinyBS.addTooltip('", buttonId, "', \"tooltip\", {\"placement\": \"right\", \"trigger\": \"click\", \"title\": \"", Tooltip, "\"}) 
                                };
                              }
                            });
                            ")))
                            }
    
    output$distPlot <- renderPlot({
    hist(rnorm(input$obs), col = 'darkgray', border = 'white')
    
    output$rendered <-   renderUI({
        checkboxGroupInput("qualdim", 
                           label = "Checkbox",
                           choiceNames  = c("cb1", "cb2"),
                           choiceValues = c("check1", "check2"),
                           selected = c("check2"))
    })
    
    output$tooltips <-   renderUI({
      list(
        makeCheckboxTooltip(checkboxValue = "check1", buttonLabel = "?", buttonId = "btn1", Tooltip = "tt1!"),
        makeCheckboxTooltip(checkboxValue = "check2", buttonLabel = "?", buttonId = "btn2", Tooltip = "tt2!")
      )
    })
    
      })
    }
    
    ui <- fluidPage(
      shinyjs::useShinyjs(),
    
      tags$head(HTML("")),
    
      # useShinyBS
    
      sidebarLayout(
    sidebarPanel(
      sliderInput("obs", "Number of observations:", min = 10, max = 500, value = 100),
      uiOutput("rendered"),
      uiOutput("tooltips")
    ),
        mainPanel(plotOutput("distPlot"))
      )
    )
    
    shinyApp(ui = ui, server = server)
    

提交回复
热议问题