Plot image on plotly plot

Hi

I'm trying to essentially recreate the last plot in this blogpost. So display an image from file onto a plotly plot.
I have modified the code from the blogpost a bit, so it also annotates the plot with a text string which is the path to the file I'm trying to display. This show the correct path, but I cannot figure out why the image never shows up.

Can anyone here point me in the right direction?
I know something similar could be done via Shiny but I would actually like to avoid that if at all possible.

Thanks!

 library(plotly)
 library(htmlwidgets)
  
  g <- ggplot(iris,
              aes(
                x = Sepal.Length,
                y = Petal.Length,
                color = Species,
                text = Species
              )) +
    geom_point() 
  p <- ggplotly(g, tooltip = "text") %>% partial_bundle()
  
  click_js <- "
    function(el, x) {
      el.on('plotly_click', function(d) {
        var txt = d.points[0].data.text;
        var image_location = '/Users/au206907/Documents/dsl/hovertest/' + txt + '.jpg';
        var img = {
          source: image_location,
          x: 4.5,
          y: 6,
          xref: 'x',
          yref: 'y',
          xanchor: 'left',
          yanchor: 'top',
          text: image_location,
          layer: 'above'
        };
        var layout_update = {
            images: [img],
            annotations: [img]
        }
        Plotly.relayout(el.id, layout_update);
      })
    }
    "
  onRender(p, click_js) 

Created on 2023-04-11 with reprex v2.0.2

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.2.2 (2022-10-31)
#>  os       macOS Ventura 13.3.1
#>  system   aarch64, darwin20
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       Europe/Copenhagen
#>  date     2023-04-11
#>  pandoc   2.19.2 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  cli           3.6.1   2023-03-23 [1] CRAN (R 4.2.0)
#>  digest        0.6.31  2022-12-11 [1] CRAN (R 4.2.0)
#>  evaluate      0.20    2023-01-17 [1] CRAN (R 4.2.0)
#>  fastmap       1.1.1   2023-02-24 [1] CRAN (R 4.2.0)
#>  fs            1.6.1   2023-02-06 [1] CRAN (R 4.2.0)
#>  glue          1.6.2   2022-02-24 [1] CRAN (R 4.2.0)
#>  htmltools     0.5.5   2023-03-23 [1] CRAN (R 4.2.0)
#>  knitr         1.42    2023-01-25 [1] CRAN (R 4.2.0)
#>  lifecycle     1.0.3   2022-10-07 [1] CRAN (R 4.2.0)
#>  magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.2.0)
#>  purrr         1.0.1   2023-01-10 [1] CRAN (R 4.2.0)
#>  R.cache       0.16.0  2022-07-21 [1] CRAN (R 4.2.0)
#>  R.methodsS3   1.8.2   2022-06-13 [1] CRAN (R 4.2.0)
#>  R.oo          1.25.0  2022-06-12 [1] CRAN (R 4.2.0)
#>  R.utils       2.12.2  2022-11-11 [1] CRAN (R 4.2.0)
#>  reprex        2.0.2   2022-08-17 [1] CRAN (R 4.2.0)
#>  rlang         1.1.0   2023-03-14 [1] CRAN (R 4.2.0)
#>  rmarkdown     2.21    2023-03-26 [1] CRAN (R 4.2.2)
#>  rstudioapi    0.14    2022-08-22 [1] CRAN (R 4.2.1)
#>  sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.2.0)
#>  styler        1.9.1   2023-03-04 [1] CRAN (R 4.2.0)
#>  vctrs         0.6.1   2023-03-22 [1] CRAN (R 4.2.0)
#>  withr         2.5.0   2022-03-03 [1] CRAN (R 4.2.0)
#>  xfun          0.38    2023-03-24 [1] CRAN (R 4.2.0)
#>  yaml          2.3.7   2023-01-23 [1] CRAN (R 4.2.0)
#> 
#>  [1] /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.