用于 pickerInput shiny js 的 R Shiny 工具提示

R Shiny tooltip for pickerInput shiny js

我正在尝试向 select 输入内容添加工具提示。我想将 txt 矢量作为工具提示,对应悬停时的输入。

Javascript 代码不是正式的,但这是我的草稿:

library(shiny)
library(shinyjs)
library(shinyWidgets)

if (interactive()) {

  ui <- fluidPage(
    useShinyjs(),
    extendShinyjs(text = '
      shinyjs.selectInput_tooltips = function(id, tooltips){
        var selectiInput = $("#"+id).closest("div").find(".dropdown-menu").get(0);
        var element_selectInput = selectiInput.childNodes;
        for(var i = 0; i< element_selectInput.length; i++){
          element_selectInput[i].title = tooltips[i];
        }
      }; 
    '),
    uiOutput("picker")

  )
  server <- function(input, output) {
    output$picker <- renderUI({
      txt <- c("Explanation 1", "Explanation 2", "Explanation 3")
      tagList(
        pickerInput(inputId = "id", label = "Value :", choices = c("num1" = 1, "num2" = 2, "num3" = 3)),
        js$selectInput_tooltips("id", txt)
      )
    })
  }
  shinyApp(ui, server)
}

可以接受任何其他方法,非常感谢任何帮助。

非常感谢。

您可以查看 extendShinyjs 的第三个示例以使用带有多个参数的函数!

library(shiny)
library(shinyjs)
library(shinyWidgets)

txt <- c("Explanation 1", "Explanation 2", "Explanation 3")
ui <- fluidPage(
  useShinyjs(),
  extendShinyjs(text = '
          shinyjs.selectInput_tooltips = function(params){

          var defaultParams = {
            id : null,
            tooltips : null
          };
          params = shinyjs.getParams(params, defaultParams);

          var selectInput = $("#"+params.id).closest("div").find(".dropdown-menu").get(1);
          var element_selectInput = selectInput.childNodes;

          if(element_selectInput.length >0 && element_selectInput[0].title == ""){ // to be trigger only once
            for(var i = 0; i< element_selectInput.length; i++){
              element_selectInput[i].title = params.tooltips[i];
            }
          }

        }; 
      '),
  pickerInput(inputId = "id", label = "Value :", choices = c("num1" = 1, "num2" = 2, "num3" = 3))

)
server <- function(input, output) {
  # throw your function when you click on pickerInput
  # Use this because if you don't click on it, the function couldn't work !
  # because choices of pickerInput doesn't exist yet
  onclick("id" ,js$selectInput_tooltips("id",txt)) 
}
shinyApp(ui, server)