Reputation: 1721
I am struggling to embed leaflet map created R into my github account
The file exported into single HTML file and I can view it on my computer
When I upload and then open from github, it gives raw HTML code, not the interactive map
And I'm not even sure how to embed this interactive map in ReadMe.md. When I place the link to the html and click it, I get the same raw map.
Help!
I created follow-up question, If HTML doesn't work well in github, how can some people make it work
Upvotes: 1
Views: 2241
Reputation: 3402
Just in case anyone is still looking for a solution, I wrote a post explaning how to do it, assuming that you use Jekyll to build your website hosted on GitHub.
https://dieghernan.github.io/201905_Leaflet_R_Jekyll/
In short,you have to perform several steps:
On GitHub/Jekyll
Identify the code you need to include. I already did it for you See on my GitHub
Copy/paste it in the code that builds your <head>
. This would depend on your Jekyll implementation. After doing that, the needed libraries would be loaded every time that Jekyll builds the website.
On Rstudio
leaflet
and knit it to a github_document
. It is important to knit it using always_allow_html: yes
in the front matter.On Markdown
leaflet
map has produced an html
code in the .md
file. Something like this:<!--html_preserve-->
<script type="application/json" data-for="htmlwidget-SOMEIDNUMBER">
{"x":{"options": ...
<!--more funky code here-->
<script>
part:<div id="htmlwidget-SOMEIDNUMBER" style="width:100%;height:216px;" class="leaflet html-widget"></div>
So you got something like this for each leaflet
map:
<!--html_preserve-->
<div id="htmlwidget-SOMEIDNUMBER" style="width:100%;height:216px;" class="leaflet html- widget"></div>
<script type="application/json" data-for="htmlwidget-SOMEIDNUMBER">
{"x":{"options": ...
<!--more funky code here-->
Now publish it on GitHub. Now when Jekyll builds your blog/web hosted on GitHub the libraries are loaded and the leaflet map displays correctly. Note that on local or markdown the map is still not visible.
Once that you have done it a couple of times you will see that is basicaly copy/paste/modify the chunk that I presented on step 5.
Upvotes: 0
Reputation: 24945
You can prepend http://htmlpreview.github.io/? to the url of where you put the html file. In your case:
Upvotes: 2