当鼠标悬停在某个区域上时,如何显示文本?

时间:2019-05-13 12:08:18

标签: r shiny

我制作了一个简单的仪表板,其中的sliderInput范围受到限制。为了解释为什么限制它,当鼠标悬停在限制区域上时,我希望显示一个文本(框)。有没有简单的方法可以做到这一点?

到目前为止,我只在绘图和图形中找到有关文本悬停的信息。

sliderInput2 <- function(inputId, label, min, max, value, step=NULL, from_min, from_max){
  x <- sliderInput(inputId, label, min, max, value, step)
  x$children[[2]]$attribs <- c(x$children[[2]]$attribs, 
                               "data-from-min" = from_min, 
                               "data-from-max" = from_max)
  x
}

ui <- fluidPage(
  sliderInput2("slider", "Slide:",
               min = 0, max = 1, value = 0.5, step = 0.1, from_min = 0.2, from_max = 0.8
  )
)

server <- function(input, output) {}

shinyApp(ui, server)

运行上述代码后,只能在0.2到0.8之间使用滑块。当尝试将其设置为例如0.9时,或者当鼠标悬停在该区域上时,我希望显示一些文本。这可能吗?

谢谢!

2 个答案:

答案 0 :(得分:1)

shinyBS库允许这样做:

shinyBS

例如,请参见下面的更新代码。

library(shiny)
library(shinyBS)

sliderInput2 <- function(inputId, label, min, max, value, step=NULL, from_min, from_max){
  x <- sliderInput(inputId, label, min, max, value, step)
  x$children[[2]]$attribs <- c(x$children[[2]]$attribs, 
                               "data-from-min" = from_min, 
                               "data-from-max" = from_max)
  x
}

ui <- fluidPage(
  sliderInput2("slider", "Slide:",
               min = 0, max = 1, value = 0.5, step = 0.1, from_min = 0.2, from_max = 0.8
  ),
  bsTooltip("slider", "HoverOnMe", placement = "bottom", trigger = "hover",
            options = NULL)
)

server <- function(input, output) {}

shinyApp(ui, server)

答案 1 :(得分:1)

您可以将其包装在div中并提供标题或使用bsTooltip

library(shiny)
library(shinyBS)

sliderInput2 <- function(inputId, label, min, max, value, step=NULL, from_min, from_max){
  x <- sliderInput(inputId, label, min, max, value, step)
  x$children[[2]]$attribs <- c(x$children[[2]]$attribs, 
                               "data-from-min" = from_min, 
                               "data-from-max" = from_max)
  x
}

ui <- fluidPage(
  tags$div(
    title="My title", 
    sliderInput2("slider", "Slide:", min = 0, max = 1, value = 0.5, step = 0.1, from_min = 0.2, from_max = 0.8)
  ),
  sliderInput2("slider2", "Slide:", min = 0, max = 1, value = 0.5, step = 0.1, from_min = 0.2, from_max = 0.8),
  bsTooltip(id = "slider2", title = "My hover text", placement = "right", trigger = "hover")
)

server <- function(input, output) {}

shinyApp(ui, server)