2

I'd like ultimately to insert R (interactive or not) graphics in the tooltips of a ggiraph. Using an iframe as I successfully did before with leaflet, it would look like this:

library(ggplot2)
library(ggiraph)
library(htmltools)

p <- {ggplot() + 
  aes(1,1, tooltip = "blabla") +
  geom_point_interactive()} %>% 
  girafe(ggobj = .)

save_html(p, file = "test.html") # I would use a loop to save many different graphs at the same time

{ggplot() + 
    aes(1,1, tooltip = HTML("<iframe src='test.html' frameborder=0 width=300 height=300></iframe>")) +
    geom_point_interactive()} %>% 
  girafe(ggobj = .)

Unfortunately, I came with somewhat of a bug. Let's try:

{ggplot() + 
    aes(1,1, tooltip = HTML("<iframe src='https://stackoverflow.com' frameborder=0 width=300 height=300></iframe>")) +
    geom_point_interactive()} %>% 
  girafe(ggobj = .)

In Firefox, it gives just a black square and in RStudio IDE it unexpectedly opens the site in a browser on mouse hover. black square tooltip

So I've got 2 questions:

  • Why iframes are not working inside a ggiraph tooltip?
  • Do you have any idea (it can be something else than iframes, of course, and hopefully more elegant) to insert R (interactive or not) graphics in the tooltips of a ggiraph (without shiny)?
doana
  • 35
  • 4

1 Answers1

1
  1. First you need to check your iframe is working before to use it in ggiraph that does not check the validity of your iframe code. The example you show does not work for me, I get a black screen, as you can get by using ggiraph. For the example, I am using ggiraph cran page.

  2. It seems RStudio viewer does not support iframe, so make sure you view your result in a modern browser (chrome, ff, safari, ...)

  3. You can use htmltools::HTML() but it's not mandatory.

library(ggplot2)
library(ggiraph)

pp <- ggplot() + 
    aes(1,1, tooltip = "<iframe src='https://cran.r-project.org/package=ggiraph'></iframe>") +
    geom_point_interactive()
girafe(ggobj = pp)

enter image description here

David Gohel
  • 9,180
  • 2
  • 16
  • 34
  • Thank you for answering the first part of my question! Do you have any idea why some iframes are not working? (I'm not very fluent in html) – doana Apr 22 '22 at 13:32
  • True, sorry, I forgot about the 2nd one. I would suggest to encode image as base64 **if not too many images** or to serve image in a server (see `servr:httd` that is quite simple), then use in tooltips images with src attributes as `http://localhost:4321/one_of_many_images.png`. It always works in shiny because shiny (by definition) provides you a server env when executing ; that is not an issue with ggiraph or rmarkdown, things don't work as easy when working with local HTML. – David Gohel Apr 22 '22 at 20:22