Simone
Simone

Reputation: 4940

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

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?

Upvotes: 4

Views: 1819

Answers (1)

cromulent
cromulent

Reputation: 71

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.

Upvotes: 7

Related Questions