Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

embed R leaflet map in wordpress

I have just created a simple interactive map using leaflet package in R. Something like this

leaflet() %>% addTiles() 

Now I would like to embed it in my wordpress website.

I clicked on the export button of RStudio Viewer and chosen "Save as web page...", then stored the .html in my local computer.

I tried to embed this map in a post in my WP website by clicking on "Add a media" in the editor of the page and the choosing the .html previously stored. But I get the error:

"1 file could not be uploaded because the file type is not supported."

I tried to open the html file in an editor and copy and paste the (very long, full of coordinates) html code into the html tab of WP page editor. The editor convert this code into

<div id="htmlwidget_container">
<div id="htmlwidget-2390" class="leaflet html-widget" style="width: 100%; height: 400px;"></div> </div>

and I don't see any map in the visual tab. I really don't know how to proceed. Any help will be appreciated. As you have noticed I am completely new to WP and web applications.

Thanks a lot, jacopo

like image 222
Jacopo Avatar asked Feb 02 '17 17:02

Jacopo


1 Answers

U can try to save the widget to a .html file and import this .html file to your media library.

library(htmlwidgets) 
library(DT) 
a <- datatable(iris) 
saveWidget(a, "datatable-iris-example.html")

Next, import the .html file to your media library. Then, add the shortcode to your post. Here's how to encode it in the page when editing the blog post:

<iframe seamless src="http://www.phillipburger.net/wordpress/wp-
content/uploads/2015/05/datatable-iris-example.html" width="100%" 
height="500"></iframe>

All credits go to Phillip Burger and his post.

Anyone got a better method?

like image 127
OB83 Avatar answered Sep 17 '22 18:09

OB83