block by timelyportfolio 8572f21cbdcae7872171

R tooltipsterR with image tooltip

Full Screen

Built with blockbuilder.org

As an extension to the examples shown in the post Week 44|tooltipsterR, I thought it might be interesting to demonstrate how to provide an R plot as a tooltip with the R htmlwidget tooltipsterR. An interactive SVG plot would be even better, so I’ll try that next.

library(tooltipsterR)
library(htmltools)
library(base64enc)

tf <- tempfile()
png(tf)
  plot(1:3)
dev.off()
bE <- base64encode(tf)
unlink(tf)

browsable(
  tagList(
    tags$div(
      id = "plot1",
      style = "height:200px;width:200px",
      tags$pre(
        "plot(1:3)"
      )
    ),
    tooltipster(),
    tags$script(HTML(sprintf(
      "
      $('#plot1').tooltipster({
        content: $('<img src=\"data:image/png;base64,%s\">'),
        arrow: false,
        offsetY: -550
      })
      ",
      bE
    )))
  )
)