67

I want to have a tool-tip in my Shiny R application. Is there any easy way to achieve that? For now, I am creating a density map and I want a simple tool-tip showing "click here to slide through years" while hovering the mouse over slider YEAR.

User Interface:

library(shiny)
shinyUI(pageWithSidebar(
  headerPanel("Density Map"),
  sidebarPanel(
    sliderInput("slider_year", "YEAR:", 
                min = 2001, max = 2011, value = 2009, 
                format="####", locale="us"
    )
  )
 ),

  mainPanel(  
    plotOutput("event_heatmap_map", width = "100%", height = "100%")
  )
))


Server Code:

library(shiny)
library(ggmap)
library(ggplot2)
mydata <- read.csv("/var/shiny-server/www/dMetrics.csv")
shinyServer(function(input, output) {
    output$event_heatmap_map <- renderPlot(width = "auto", height = 640,{

        slice_year <- mydata[mydata$YEAR==input$slider_year,]
        map <- get_map(c(lon = -55.3632715, lat = 31.7632836), zoom = 3, source = 'google', maptype = c("terrain"), messaging = FALSE, color = 'color')
        world <- ggmap(map)
        world <- world + stat_density2d(data = slice_year, aes(x = WEST, y = NORTH, fill = ..level.., alpha = ..level..), show_guide = FALSE, geom = "polygon", na.rm = TRUE) + scale_fill_gradient(name="Density", low="maroon", high="yellow", guide = 'colorbar')
        plot(world)
    })
})

Thanks for the help.

L. Bakker
  • 147
  • 1
  • 13
Sabin
  • 11,662
  • 3
  • 25
  • 39

3 Answers3

72

I think you should be able to replace this:

sliderInput("slider_year", "YEAR:", 
            min = 2001, max = 2011, value = 2009, 
            format="####", locale="us"
)

with this:

tags$div(title="Click here to slide through years",
    sliderInput("slider_year", "YEAR:", 
                min = 2001, max = 2011, value = 2009, 
                format="####", locale="us"
    )
)
Joe Cheng
  • 8,001
  • 42
  • 37
  • Thank you very much for the quick reply. Shiny R is really cool. I am also using Google Motion Chart with Shiny. Can you please do one more favor for me by providing me a way to add/change tool-tip in there. I haven't shown anything in Ui.r and in server.r I put some codes like: `output$view_gviz <- renderGvis({ chart <- gvisMotionChart(visdata, idvar="Storm", timevar="Year",......)`. Thank you once again for the help. – Sabin May 09 '13 at 17:43
  • I will be grateful if anyone provides me with the hint for my another post, similar to this one: [post](http://stackoverflow.com/q/16886892/2167517) – Sabin Jun 03 '13 at 02:37
  • But is there a way to style the title so that it scrolls if the tooltip is larger than a certain size? – bokov Jun 27 '13 at 16:29
  • The div tag sends the encapsulated input object onto another row. Is there a way of keeping it inline? For instance, using a tag other than div? – RDavey Apr 29 '20 at 12:21
54

This is slightly easier and more elegant way.

library(shinyBS) # Additional Bootstrap Controls

## From ui.R: Adds a tooltip to element with inputId = "someInput" 
## with text, "This is an input.", that appears to the left on hover.
bsTooltip(id = "someInput", title = "This is an input", 
          placement = "left", trigger = "hover")

## From server.R: Add the same tooltip as above
addTooltip(session, id = "someInput", title = "This is an input.",
           placement = "left", trigger = "hover")

You can add the Tooltip in ui.R or server.R, Additional you can also use Popover.

micstr
  • 5,080
  • 8
  • 48
  • 76
  • 2
    I'm not sure adding another library is easier or more elegant, although shinyBS is still great. – dca Jan 05 '21 at 23:22
1

Nowadays it's possible to wrap ggplot visualizations around plot_ly, and plotly has a native component (precissely named tootip) which will serve the purpose you're looking for:

Example taken from: https://plotly.com/ggplot2/interactive-tooltip/
library(plotly)
#install.packages("gapminder")
library(gapminder)

p <- ggplot(gapminder, aes(x = gdpPercap, y = lifeExp, color = continent, text =      paste("country:", country))) +
     geom_point(alpha = (1/3)) + scale_x_log10()  

fig <- ggplotly(p)

fig

Also, refer to: https://plotly-r.com/controlling-tooltips.html for a more detailed explanation.

Dan Valle
  • 31
  • 4