【问题标题】:R Shiny tooltip for pickerInput shiny js用于pickerInput闪亮js的R闪亮工具提示
【发布时间】:2020-02-18 16:13:12
【问题描述】:

我正在尝试添加工具提示来选择输入内容。我希望将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)
}

可能会接受任何其他方式,非常感谢任何帮助。

非常感谢。

【问题讨论】:

    标签: javascript r shiny picker shinyjs


    【解决方案1】:

    您可以查看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)
    

    【讨论】:

    • 完美运行!谢谢。接下来我会玩一些css
    猜你喜欢
    • 2013-05-03
    • 1970-01-01
    • 2017-03-15
    • 2020-07-28
    • 2017-03-06
    • 1970-01-01
    • 1970-01-01
    • 2015-11-04
    相关资源
    最近更新 更多