当我尝试通过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
我收到此错误:
HTML widgets cannot be represented in plain text (need html)
.
正如这里建议的那样,我已经尝试过:
library(plotly)
embed_notebook(m)
但我得到:
Error in UseMethod("embed_notebook"): no applicable method for 'embed_notebook' applied to an object of class "c('leaflet', 'htmlwidget')
我怎样才能绘制这种图表?
embed_notebook是专门为绘图对象定义的。我会浏览文档,看看传单是否具有自己的等效功能。
或者,由于它是一个 html 小部件,您可以将其另存为 html 文件,然后将该文件嵌入到笔记本的 iframe 中。这可以通过类似的东西来实现
library(IRdisplay)
htmlwidgets::saveWidget(m, "m.html")
display_html('<iframe src="m.html" width=100% height=450></iframe>')
如果您不想在文件夹中保留一堆html文件,也可以将小部件的原始html输入到iframe中,然后使用以下命令将其删除
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")
但我发现这会在最新版本的 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)
}
但同样,这可能不适用于Chrome。