rggplot2ggiraphhtmltools

How to to insert R (interactive or not) graphics in the tooltips of a ggiraph?


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:


Solution

    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