Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: "HTML widgets cannot be represented in plain text"

Tags:

r

jupyter

When I try to run this by Jupyter:

library(leaflet)

m <- leaflet() %>%
  addTiles() %>%  # Add default OpenStreetMap map tiles
  addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R")
m  # Print the map

I get this error:

HTML widgets cannot be represented in plain text (need html).

As suggested here I have tried:

library(plotly)
embed_notebook(m)

but I get:

Error in UseMethod("embed_notebook"): no applicable method for 'embed_notebook' applied to an object of class "c('leaflet', 'htmlwidget')

How could I plot this kind of graph?

like image 999
Simone Avatar asked Mar 08 '23 19:03

Simone


1 Answers

embed_notebook is specifically defined for plotly objects. I would look through the documentation to see if leaflet has its own equivalent function.

Alternatively, since it's an html widget, you can save it as an html file, then embed that file inside of an iframe in your notebook. This can be accomplished with something like

library(IRdisplay)
htmlwidgets::saveWidget(m, "m.html")
display_html('<iframe src="m.html" width=100% height=450></iframe>')

If you don't want to keep a bunch of html files in your folder, you can also enter the raw html of your widget into your iframe then delete it using

rawHTML = base64enc::dataURI(mime = "text/html;charset=utf-8", file = "m.html")
display_html(paste("<iframe src=", rawHTML, "width=100% height=450></iframe>", sep = "\""))
unlink("m.html")

But I've found that this generates an error with the most recent version of Chrome.

If it helps, I cobbled together the following function from the source code of embed_notebook

embed = function(x, height) {
    library(IRdisplay)
    tmp = tempfile(fileext = ".html")
    htmlwidgets::saveWidget(x, tmp)
    rawHTML = base64enc::dataURI(mime = "text/html;charset=utf-8", file = tmp)
    display_html(paste("<iframe src=", rawHTML, "width=100% height=", height, "id=","igraph", "scrolling=","no","seamless=","seamless", "frameBorder=","0","></iframe>", sep = "\""))
    unlink(tmp)
}

But again, this may not work for Chrome.

like image 99
cromulent Avatar answered Mar 10 '23 09:03

cromulent