This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

You can also embed maps, for example:

library(leaflet)
library(rgdal)
## Loading required package: sp
## rgdal: version: 1.0-4, (SVN revision 548)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 1.11.2, released 2015/02/10
##  Path to GDAL shared files: /Library/Frameworks/R.framework/Versions/3.2/Resources/library/rgdal/gdal
##  Loaded PROJ.4 runtime: Rel. 4.9.1, 04 March 2015, [PJ_VERSION: 491]
##  Path to PROJ.4 shared files: /Library/Frameworks/R.framework/Versions/3.2/Resources/library/rgdal/proj
##  Linking to sp version: 1.1-1
collines <- readOGR("BDI_Admin4_region.shp", layer = "BDI_Admin4_region", verbose = FALSE)
collines$NUM_PATIENTS <- as.numeric(as.character(collines$NUM_PATIEN))

#set metric to map by
filterBy <- "NUM_PATIENTS"

#take subset of data to map by, based on filterBy
collines.subset <- subset(collines, collines@data[[filterBy]]>0)

#create function to build the color scheme

  
  #set color scheme
  colorScheme <- c("#851811","#ab2218","#e3d389","#7dc988")
  
  #create bins
  bins.quantile <- quantile(collines.subset@data[,filterBy][collines.subset@data[,filterBy]!=0], c(0, .2, 0.4,0.6, 0.8, 0.9,0.98,  1),na.rm = TRUE) %>% round() %>% unique()
  
  #make color pallete from bins
  palette <- colorBin(colorScheme, collines.subset@data[,filterBy], bins=bins.quantile, na.color = "#ab2218")

#create popup content
popupContent <- paste("<strong>",collines.subset@data[,"COLLINE"],"</strong><br>", 
                            filterBy,": ",collines.subset@data[,filterBy],
                            sep="")


#print map
leaflet() %>% 
        addTiles(urlTemplate = "https://api.tiles.mapbox.com/v4/mapbox.light/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoiYWxleGdvb2RlbGwiLCJhIjoicy1fNU4wZyJ9.PjqkYLHj6u5PLiWH61TQDw") %>%
  addPolygons(data=collines, stroke = TRUE, weight=1, fillOpacity = 0, smoothFactor = 0.5, color= "#a0a1a0",
                    popup=paste("<strong>",collines@data[,"COLLINE"],"</strong><br>No data available.")
                    ) %>%
  addPolygons(data=collines.subset, stroke = FALSE, fillOpacity = 0.5, smoothFactor = 0.5,
                    fillColor= palette(collines.subset@data[,filterBy]),
                    popup = ~popupContent
                    ) %>%
  addLegend(position = "bottomright",
            pal = palette,
            values = collines.subset@data[,filterBy],
            title=paste(filterBy, "Per Colline"))