Embedding a R - Leaflet map on a jekyll site hosted on Github - r

I have a website here hosted on Github Pages using Beautiful Jekyll. See here for the actual code behind the blog on Github.
I'd like to embed a leaflet map into one of my blog posts. I am using the R package leaflet.
So say for example I make a map:
library(htmlwidgets)
library(leaflet)
m <- leaflet() %>% #basic map
addTiles() %>%
How do I embed this on my page? One thing I tried to do is save the html file using:
saveWidget(widget = m, file="your_map.html", selfcontained = FALSE)
This works and saves the html file locally. I tried copying and pasting the html code into one of _posts on Github but that didn't work.
Any suggestions?

You can have a look at how they are doing this for the official R leaflet page at their github page (the one for the website, not for the leaflet-code).
I don't see on your site that you are using R-markdown to create your markdown pages. It is really worth a try! Have a look at yihui's jekyll example. Basically you use knitr to generate your pages. This means you write markdown and are able to embed your R-Code into the markdown page. Like for example:
```{r}
plot(runif(10))
```
The same you can do with leaflet:
```{r}
library(leaflet)
leaflet() %>% addTiles() %>% setView(10, 51, zoom = 8)
```
htmlwidgets should be automatically loaded by knitr.
For example you can have a look how the basemap page for the R leaflet page is done in R-markdown.
One important thing if you include leaflet maps in your page is though to include all the scripts in the page head. On my page I inserted it in the head.html in the _includes folder.
I had to add the following:
{% if page.maps %}
<script src="/assets/jquery-1.11.1/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="/assets/bootstrap-3.3.1/css/bootstrap.min.css" rel="stylesheet" />
<script src="/assets/bootstrap-3.3.1/js/bootstrap.min.js"></script>
<script src="/assets/bootstrap-3.3.1/shim/html5shiv.min.js"></script>
<script src="/assets/bootstrap-3.3.1/shim/respond.min.js"></script>
<script src="/assets/htmlwidgets-0.5/htmlwidgets.js"></script>
<link href="/assets/leaflet-0.7.3/leaflet.css" rel="stylesheet" />
<script src="/assets/leaflet-0.7.3/leaflet.js"></script>
<link href="/assets/leafletfix-1.0.0/leafletfix.css" rel="stylesheet" />
<script src="/assets/leaflet-binding-1.0.1/leaflet.js"></script>
{% endif %}
I added an option maps: true into the front matter of pages with maps so that the part {% if page.maps %} makes sure that all this code is only loaded for pages that need it.
I copied all the scripts from a folder that was created by knitr when I compiled the R-markdown page without jekyll. Therefore I had to set
output:
html_document:
self_contained: no
in the front matter (otherwise the code is embedded in the page and can't be copied so easily).
The team behind R leaflet include the libs in their index.html like this:
<script src="libs/jquery/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="libs/bootstrap/css/flatly.min.css" rel="stylesheet" />
<script src="libs/bootstrap/js/bootstrap.min.js"></script>
<script src="libs/bootstrap/shim/html5shiv.min.js"></script>
<script src="libs/bootstrap/shim/respond.min.js"></script>
<script src="libs/htmlwidgets/htmlwidgets.js"></script>
<link href="libs/leaflet/leaflet.css" rel="stylesheet" />
<script src="libs/leaflet/leaflet.js"></script>
<link href="libs/leafletfix/leafletfix.css" rel="stylesheet" />
<script src="libs/leaflet-binding/leaflet.js"></script>
You can copy the libs from their libs folder.
I see on your page that you already got it to work with a different method. I just wanted to include this because I was just working on the same topic...

Based on #nnn answer I put together a tutorial for doing so:
FULL TUTORIAL ON THIS LINK
In short:
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
Create a map with 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
Check that every leafletmap has produced an html code in the .mdfile. Something like this:
<!--html_preserve-->
<script type="application/json" data-for="htmlwidget-SOMEIDNUMBER">
{"x":{"options": ...
<!--more funky code here-->
Paste this chunk before the <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.

I found a solution here https://jayrobwilliams.com/posts/2020/09/jekyll-html
Store the output from the saveWidget in your website directory.
wrap the relative path in an iframe and style as you please.
<iframe src="/interactiveDocs/map.html" height="600px" width="100%" style="border:none;"></iframe>
It has worked well for my purposes

Related

Quarto with a CSS in the parent directory

I want my CSS file to be shared by several Quarto R presentations, so this CSS file should be placed in a top directory. But Quarto seems to allow CSS files only in the same directory, or in a child directory. I have tried all these configurations without success, starting with the css option in the YAML header:
---
title: "my title"
author: "my name"
format:
revealjs:
css: ...
---
../styles.css
"../styles.css"
..\styles.css
"..\styles.css"
symlink 'style.css'
https://raw.githubusercontent.com/zoometh/thomashuet/master/teachings/stats/styles.css
"https://raw.githubusercontent.com/zoometh/thomashuet/master/teachings/stats/styles.css"
https://github.com/zoometh/thomashuet/blob/main/teachings/stats/styles.css
C:/styles.css
"C:/styles.css"
But also outside the YAML, in the narrative parts and code chunks
<link href="../styles.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" type="text/css" href="C:/Rprojects/thomashuet/teachings/stats/styles.css">
<link href="https://raw.githubusercontent.com/xxx/xxx/master/teachings/stats/styles.css" rel="stylesheet">
``{css}
<link href="https://raw.githubusercontent.com/zoometh/thomashuet/master/teachings/stats/styles.css" rel="stylesheet" type="text/css">
``
<link rel="stylesheet" type="text/css" href="C:/Rprojects/thomashuet/teachings/stats/styles.css">
So relative paths, absolute paths (local or GitHub), symbolic links, calls outside the YAML header, nothing works.
To share a common CSS stylesheet among several RevealJs presentations, using a CSS stylesheet stored in a GitHub repo seems a good idea.
But using raw.githubusercontent to link the stylesheet will not work, because raw.githubusercontent makes all files use the text/plain MIME type, even if the file is a CSS or JavaScript file. Therefore, the following will not work.
<link href="https://raw.githubusercontent.com/zoometh/thomashuet/master/teachings/stats/styles.css" rel="stylesheet" type="text/css">
One possible solution to this problem is to use jsDelivr.net (A free CDN for open-source projects). We can follow the steps described in this answer,
Steps:
Find your link on GitHub, and click to the "Raw" version.
Copy the URL. In your case which is, https://raw.githubusercontent.com/zoometh/thomashuet/main/teachings/stats/styles.css.
Change raw.githubusercontent.com to cdn.jsdelivr.net.
Insert /gh/ before your username.
Remove the branch name (main in this case).
So the final URL would look like, https://cdn.jsdelivr.net/gh/zoometh/thomashuet/teachings/stats/styles.css. Now you can use this URL within a link tag in a html file and then include the html file using the include-in-header yaml key in the qmd file.
style.html
<link href="https://cdn.jsdelivr.net/gh/zoometh/thomashuet/teachings/stats/styles.css" rel="stylesheet" type="text/css">
presentation.qmd
---
title: "my title"
author: "my name"
format:
revealjs:
include-in-header: style.html
---
Another good solution to this problem is to use Github pages which will host your repo at a special URL like https://‹your_github_userName›.github.io/‹repo›. And you can use this URL to point to a specific folder/files in your repo.

Bokeh autoload_static still interactive

I'm assuming I'm doing something wrong as when I attempt to use Bokeh's 'autoload_static' function and place the script tag in my html file the graph is still interactive? In addition to this, the output of my script tag (by autoload static) doesn't look exactly the same as tutorial despite it being the same code...
Would really appreciate the help. I'm trying to output it as static so I can correctly convert it to pdf with pdfkit - which unfortunately doesn't work with interactive graphs.
Thanks!
Html :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Bokeh Scatter Plots</title>
<!-- <link rel="stylesheet" href="http://cdn.bokeh.org/bokeh/release/bokeh-0.12.7.min.css" type="text/css" /> -->
<!-- <script type="text/javascript" src="http://cdn.bokeh.org/bokeh/release/bokeh-0.12.7.min.js"></script> -->
</head>
<body>
<script
src="js-outputted-by-autoload_static"
id="f9632bd4-873b-4c08-a4ad-c8a997873430"
data-bokeh-model-id="bec3e18b-71d0-4d3d-9d6a-0079d8fc6082"
data-bokeh-doc-id="b39e1b50-1e37-4062-92a8-888cc4424328"
></script>
</html>
Bokeh:
from bokeh.resources import CDN
from bokeh.plotting import figure
from bokeh.embed import autoload_static
plot = figure()
plot.circle([1,2], [3,4])
js, tag = autoload_static(plot, CDN, "js-outputted-by-autoload_static")
autoload_static is not for generating images, it is for generating JavaScript files that can embed standard interactive Bokeh plots in web pages. The "static" part refers to the fact that these plots are not backed by a Bokeh server.
Since autoload_static still generates JavaScript to render onto an HTML canvas, I doubt it will be useful at all with pdfkit (which I assume cannot do anything with JS code).
If you want to create images (e.g. PNGs) of Bokeh plots, you should look at the Exporting Plots section of the User's Guide. With recent versions of Bokeh, you can do e.g.
from bokeh.io import export_png
export_png(plot, filename="plot.png")
to generate a PNG (which presumably is what pdfkit can handle). Some optional dependencies are required to be installed to use this functionality, the linked User's Guide has all the information.

Embed leaflet map created by R into github

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!
Update
I created follow-up question, If HTML doesn't work well in github, how can some people make it work
You can prepend http://htmlpreview.github.io/? to the url of where you put the html file. In your case:
http://htmlpreview.github.io/?https://github.com/Sonihal/yorbalinda_interactive_map/blob/master/web_Yorba_Linda.html
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
Create a map with 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
Check that every leafletmap has produced an html code in the .mdfile. Something like this:
<!--html_preserve-->
<script type="application/json" data-for="htmlwidget-SOMEIDNUMBER">
{"x":{"options": ...
<!--more funky code here-->
Paste this chunk before the <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.

CSS is missing when rendering an html page with codeigniter

I'm aware this is similar to numerous existing posts but after looking at a number of others I wasn't able to solve my problem. I'm attempting to load a folder full of html files I've been supplied with. I didn't write them but I have been modifying them to integrate them into an existing system.
I've tried three ways of opening them, with varying success:
1) Simply right click on 'index.html' and go 'open with' and select a browser. This works perfectly!
2) Place the whole folder contents, unchanged, onto my server under 'public_html/cat/html' and navigating to the url 'localhost/cat/html/index.html'. This returns a '404 page not found error'.
3) The strange one. Place the html files in the 'application/view' folder, separate the included css and javascript files and place them in existing folders 'public_html/css' and 'public_html/js' and update the links to them in the html files appropriately. These now look something like:
<link type="text/css" href="css/cat/style.css" rel="stylesheet" />
If I load this page by running a function that uses CodeIgniter's $this->load->view(...) function it finds the index file but loads it with no css and with broken links to the other pages. I've explored the page source and seen that the link is exactly as above but clicking gives an error that reads "404 Not Found...The requested URL /css/cat/style.css was not found on this server".
I've attempted to use the base_url() function (and site_url()) like this:
<link type="text/css" href="<? base_url('css/cat/style.css') ?>" rel="stylesheet" />
and it yields the same result on the surface but examining the page source reveals the link line has become:
<link type="text/css" href="" rel="stylesheet" />
which seems to be even worse!
Any hints?
Thanks for reading
Try the following:
<link type="text/css" href="<?php echo base_url('css/cat/style.css') ?>" rel="stylesheet" />
base_url() functon simply return value, you should take a care for displaying returned value to output! :-)
Have you correctly configure you config file in codeIgniter ?
Or shorthand
<link type="text/css" href="<?=base_url('css/cat/style.css') ?>" rel="stylesheet" />
Then view your page source and copy the link from there to your browser, see if you can access it.
Can try this code <link type="text/css" href="<?=base_url('css/cat/style.css');?>" rel="stylesheet" />
if it doesn't work, you could try <link type="text/css" href="<?=base_url('css/cat');?>/style.css" rel="stylesheet" />

lightbox and slimbox2 doesn't work with composite c1

I'm trying to use both lightbox and slimbox with Composite c1 v3.
Neither works.
I have installed the relevant packages and I have set correctly the parameters for both functions.
The result is that when I use lightbox both thumbnail and the picture appears on the page.
When I use slimbox2, the thumbnail is fine but the picture opens to a new page.
I have tried it both with IE 9 and firefox 9.0.1 with the same result.
As far as Composite C1 is concerned, I'm working on Omnicorp example site.
Anyone with the some kind of problem?
I just tried the Slimbox2 package and it worked for me in Chrome and IE. I had success installing the package, going to a page in Content, and inserting the function. Then I picked the Media Folder parameter, and set it to be a constant and selected a folder path. Once published, the slimbox behaved as expected. I am not working with the Omnicorp demo, however.
The behavior sounds to me like some part of the script isn't loading or is loading in the wrong order. Does the Omnicorp site, or another package have conflicting jQuery import(s) in the head section of a published page?
Looks like the packages have a bug, when site is running in sub-folder the javascripts files are not loaded.
Do you run your site in subfolder?
Try to fix this.
Go to Functions perspective -> Xslt functions -> locate function Composite.Media.ImageGallery.Slimbox2 -> edit its template and fix links to the files, add ~ at the begin:
<script id="slimbox2-js" src="~/Frontend/Composite/Media/ImageGallery/Slimbox-2.04/js/slimbox2.js" type="text/javascript"></script>
<link id="slimbox-2.04" media="screen" type="text/css" href="~/Frontend/Composite/Media/ImageGallery/Slimbox-2.04/css/slimbox2.css" rel="stylesheet" />
also locate function Composite.Web.Html.Lightbox, edit its template and fix links to the files (add ~):
<script id="html-lightbox-js" src="~/Frontend/Composite/Web/Html/Lightbox/js/jquery.html-lightbox.js" type="text/javascript"></script>
<link id="html-lightbox-css" media="screen" type="text/css" href="~/Frontend/Composite/Web/Html/Lightbox/css/html-lightbox.css" rel="stylesheet" />

Resources