r-用于帮助文本的闪亮UI中的工具提示



我想放置复选框标签的帮助文本作为工具提示。在下面的示例中,我使用了shinyBS包,但我只能让它用于复选框输入组的标题。

有什么想法吗?

library(shiny)
library(shinyBS)
 server <- function(input, output) {
  output$distPlot <- renderPlot({
    hist(rnorm(input$obs), col = 'darkgray', border = 'white')
  output$rendered <-   renderUI({
    checkboxGroupInput("qualdim",  tags$span("Auswahl der Qualitätsdimension",   
      tipify(bsButton("pB2", "?", style = "inverse", size = "extra-small"),
             "Here, I can place some help")),
                       c("Lernerfolg"             = "Lernerfolg"   , 
                         "Enthusiasmus"           = "Enthusiasmus"          
                         ),
                       selected = c("Lernerfolg"))

  })
  })
}
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      sliderInput("obs", "Number of observations:", min = 10, max = 500, value = 100),
      uiOutput("rendered")
    ),
    mainPanel(plotOutput("distPlot"))
  )
)
shinyApp(ui = ui, server = server)

遗憾的是,这就是其中一个时刻,闪亮的东西隐藏了大部分结构,这使得你很难把你想要的东西放在正确的地方。

但和大多数时候一样,一些JavaScript会起作用。我为您编写了一个函数,它在正确的位置插入bsButton,并调用shinyBS函数来插入工具提示。(我主要重构了tipifybdButton的功能。)使用该功能,您可以轻松地修改工具提示,而无需进一步了解JavaScript。

如果你想了解更多细节,只需在评论中询问即可。

注意:当您提到复选框时,使用它的值(发送到input$qualdim的值)

library(shiny)
library(shinyBS)
server <- function(input, output) {
  makeCheckboxTooltip <- function(checkboxValue, buttonLabel, Tooltip){
    script <- 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, "'){
                var buttonID = 'button_' + Math.floor(Math.random()*1000);
                var button = document.createElement('button');
                button.setAttribute('id', buttonID);
                button.setAttribute('type', 'button');
                button.setAttribute('class', 'btn action-button btn-inverse btn-xs');
                button.appendChild(document.createTextNode('", buttonLabel, "'));
                input.parentElement.parentElement.appendChild(button);
                shinyBS.addTooltip(buttonID, "tooltip", {"placement": "bottom", "trigger": "hover", "title": "", Tooltip, ""}) 
              };
            }
          });
        ")))
     htmltools::attachDependencies(script, shinyBS:::shinyBSDep)
  }
  output$distPlot <- renderPlot({
    hist(rnorm(input$obs), col = 'darkgray', border = 'white')
    output$rendered <-   renderUI({
      list(
        checkboxGroupInput("qualdim",  tags$span("Auswahl der Qualitätsdimension",   
          tipify(bsButton("pB2", "?", style = "inverse", size = "extra-small"), "Here, I can place some help")),
          choices = c("Lernerfolg" = "Lernerfolg", "Enthusiasmus" = "Enthusiasmus"),
          selected = c("Lernerfolg")),
        makeCheckboxTooltip(checkboxValue = "Lernerfolg", buttonLabel = "?", Tooltip = "Look! I can produce a tooltip!")
      )
    })
  })
}
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      sliderInput("obs", "Number of observations:", min = 10, max = 500, value = 100),
      uiOutput("rendered")
    ),
    mainPanel(plotOutput("distPlot"))
  )
)
shinyApp(ui = ui, server = server)

编辑:

添加了ShinyBS依赖项,以便将用于ShinyBS的JavaScript API加载到网站中。以前,由于对bsButton的另一个调用,这种情况(或多或少是意外的)发生了。

编辑2:更多精彩

因此,这个JavaScript非常好,但有点容易出错,需要开发人员具备一些额外的语言技能。

在这里,我提出了另一个答案,灵感来自@CharlFrancoisMarais,它只在R内部起作用,使事物比以前更加集成。

主要内容包括:checkboxGrouInput的扩展函数,它允许向每个Checkbox元素添加任何元素。在那里,可以像在普通标记中一样自由放置bsButton和工具提示,并且支持所有函数参数。

其次,对bsButton进行扩展,使其正确放置。这更像是一个定制的东西,只适用于@CharlFrancoisMarais的请求。

我建议您仔细阅读Shiny元素操作,因为这在R级别上提供了很多自定义。我有点兴奋。

以下完整代码:

library(shiny)
library(shinyBS)
extendedCheckboxGroup <- function(..., extensions = list()) {
  cbg <- checkboxGroupInput(...)
  nExtensions <- length(extensions)
  nChoices <- length(cbg$children[[2]]$children[[1]])
  if (nExtensions > 0 && nChoices > 0) {
    lapply(1:min(nExtensions, nChoices), function(i) {
      # For each Extension, add the element as a child (to one of the checkboxes)
      cbg$children[[2]]$children[[1]][[i]]$children[[2]] <<- extensions[[i]]
    })
  }
  cbg
}
bsButtonRight <- function(...) {
  btn <- bsButton(...)
  # Directly inject the style into the shiny element.
  btn$attribs$style <- "float: right;"
  btn
}
server <- function(input, output) {
  output$distPlot <- renderPlot({
    hist(rnorm(input$obs), col = 'darkgray', border = 'white')
    output$rendered <-   renderUI({
      extendedCheckboxGroup("qualdim", label = "Checkbox", choiceNames  = c("cb1", "cb2"), choiceValues = c("check1", "check2"), selected = c("check2"), 
                              extensions = list(
                                tipify(bsButtonRight("pB1", "?", style = "inverse", size = "extra-small"),
                                       "Here, I can place some help"),
                                tipify(bsButtonRight("pB2", "?", style = "inverse", size = "extra-small"),
                                       "Here, I can place some other help")
                              ))
    })
  })
}
ui <- fluidPage(
  shinyjs::useShinyjs(),
  tags$head(HTML("<script type='text/javascript' src='sbs/shinyBS.js'></script>")),
  # useShinyBS
  sidebarLayout(
    sidebarPanel(
      sliderInput("obs", "Number of observations:", min = 10, max = 500, value = 100),
      uiOutput("rendered")
    ),
    mainPanel(plotOutput("distPlot"))
  )
)
shinyApp(ui = ui, server = server)

这里有一个细微的更改-只在复选框中添加工具提示。

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("<script type='text/javascript' src='sbs/shinyBS.js'></script>")),
  # 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)

相关内容

  • 没有找到相关文章

最新更新