Michieldo
Michieldo

Reputation: 189

How to make text appear when mouse hovers over an area?

I have made a simple dashboards in which the range of the sliderInput is restricted. To explain why it is restricted I would like a text(box) to appear when the mouse hovers over the restricted area. Is there a simple way to do this?

So far, I have only found information about text hovers in plots and graphs.

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)

When the code above is ran, the slider can only be used from 0.2 to 0.8. When it is tried to set at for example 0.9 or when the mouse hovers over that area, I would like some text to appear. Is this possible?

Thanks!

Upvotes: 0

Views: 1131

Answers (2)

ismirsehregal
ismirsehregal

Reputation: 33407

You can wrap it in a div and provide a title or use 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)

Upvotes: 1

olorcain
olorcain

Reputation: 1248

The library shinyBS allows this:

shinyBS

For your example, see updated code below.

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)

Upvotes: 1

Related Questions