错误:"HTML小部件无法以纯文本表示"

Sim*_*one 3 r jupyter

当我试图通过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
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

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

正如我在这里建议的那样,

library(plotly)
embed_notebook(m)
Run Code Online (Sandbox Code Playgroud)

但我得到:

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

我怎么能绘制这种图表?

小智 5

embed_notebook是专门为plotly对象定义的.我会查看文档,看看传单是否有自己的等效函数.

或者,因为它是一个html小部件,你可以将它保存为html文件,然后将该文件嵌入到笔记本中的iframe中.这可以用类似的东西来完成

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

如果你不想在你的文件夹中保留一堆html文件,你也可以在你的iframe中输入你的小部件的原始html,然后使用它删除它

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")
Run Code Online (Sandbox Code Playgroud)

但我发现这会在最新版本的Chrome中产生错误.

如果它有帮助,我从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)
}
Run Code Online (Sandbox Code Playgroud)

但同样,这可能不适用于Chrome.