Creating a Leaflet Map and publishing it on RPubs

Steps:

  1. Create an RPubs account.
  2. Open Rstudio.
  3. Create a new RMarkdown document in RStudio.
  4. Create the Leaflet map in an r code block in your RMarkdown file.
  5. Knit the rmarkdown document by clicking on the Knit HTML link in RStudio.
  • When you knit and RMarkdown doc and the output of that doc is specified as html_document then RStudio will create an HTML file in your current working directory.
  1. Click on the Publish or Republish button in the resultant markdown preview window to upload and publish the HTML file on RPubs.org.
  2. Notes: If you don’t want anything on the page except for the map then your markdown file should only have the r code block and the following header at the top of the R Markdown file.

---
output: html_document
---

Sample leaflet code block

library(leaflet)
leaflet() %>% addTiles() %>% addMarkers(lng = -122.258, lat = 37.870)

Sharing your map

You map will know be publically available to anyone with a web browser via RPubs. You can get the URL in the address bar of the browser window in which your map opens as soon as you click publish. For example the URL for this document is: http://rpubs.com/pfrontiera/234119.

You can copy and paste your URL into a blog post or another web page as an inline link to display your map in another context. For example, I would embed the following to link to this RPub document:

<iframe src="http://rpubs.com/pfrontiera/234119" width="90%" height="100%" scrolling="no"></iframe>

That works best if there is nothing in your RMarkdown dock except the map, which you can see in this example:

Another Way to Share

Now that’s not great but it is a start. If you want better control, you can:

  1. Create a GitHub GIST account

  2. Upload the HTML file that RStudio outputs when you click Knit HTML to your GIST account

  3. Publish your new GIST by clicking Create Public GIST
  • Change the file name of your GIST to index.html. You can have more than one index.html file on your GIST account.
  1. Take a look at the URL of your GIST, for example:
  1. Open that URL in bl.ocks.org to view it as a rendered web page.

As with most things R, there are many ways to share your work online. This doc shows a few options to get you started.

Last updated 12/9/2016