publishing web maps using leaflet package in RStudio

Some GI analyst may find it a cumbersome process to write long lines of code to create HTML pages containing leaflet maps. A good alternative is to create and publish these maps using leaflet in R package supported by the RStudio.

This post will demonstrate how to use the RStudio application to publish web maps using leaflet R package.

Install RStudio first. Then install the Raster and Leaflet package onto the R application. This demonstration will need the raster package to load in our vector files as sp-SpatialPoints and SpatialPolygons and leaflet package to initialize a leaflet map in R.

1. Launch RStudio. Go to the packages tab and click on the raster package checkbox. You would notice the package is loaded on the console window. See screenshot below.


2. Do the same for the leaflet package box to load it into the console window.


3. Next we load our vector data into the application from the local directory. The red boxed section on the window console illustrates our working example. The R statement loads a shapefile from the local directory C.


4. When loaded successfully, the file will be displayed in the environment window. See screenshot below for illustration. Click on the blue dropdown arrow button to check out the attributes of the layer.


5. Next we head to the console and write out the lines that would draw the leaflet map and add vector data.

leaflet(data= delta) %>%    

This will initialize the leaflet map and load the data which is delta in this instance  

 addTiles() %>%    

add a base map, default is OSM

addPolygons(CSS styles)    function adds polygon features and you can add style options as is the case in our example red boxed on the window console of the screenshot shown below.


Hit the enter button to run the code.

6. Click on the viewer tab to see a preview of the leaflet map.


7. Next we decide to add popup and change the base map tiles from default OSM to tiles from CartoDB. The addTiles() now becomes addProviderTiles(“CartoDB.Positron) and within the addPolygons(), we now add popup=paste0(“Name:”, as.character(delta$name_2). Click on Enter to run the new code.


8. Click on the viewer tab to see the preview. You can click on any of the features to see the popup display.


9. Next we publish the map just created. In the viewer page, click on Publish (red boxed on the screenshot below).


10. Fill in login credentials.To publish, user must have a RPub account.


11. Fill in the document details


12. The leaflet map is now published as a web map. User can copy the browser address and use the URL to share the map to others or other websites. See live map at


Thank you for following the blog.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s