2017-10-16 35 views
0

当我试图通过Jupyter运行此:错误: “HTML控件不能以纯文本格式表示的”

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)

至于建议here我曾尝试:

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')

我怎么能画出这样的图呢?

回答

1

embed_notebook是专为plotly对象定义的。我会查看文档以查看传单是否具有自己的等效功能。

或者,由于它是一个html小部件,因此可以将其另存为html文件,然后将该文件嵌入笔记本中的iframe中。这可以用类似

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

来完成。如果你不想让一堆的HTML文件的文件夹中,也可以输入你的widget的原始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工作。

+1

#cromulent问题是'htmlwidgets :: saveWidget(m,“m.html”)'doesen't创建html文件。 – Simone

+0

@Simone如果这是侮辱,我很抱歉,但是您的R版本中是否安装了htmlwidgets软件包?如果你这样做,我还没有足够的经验去了解可能导致问题的原因。 – cromulent

+0

是的,我已经安装了'htmlwidgets'软件包 – Simone