R setting

R is always pointed at a directory on our computer. We can find out which directory by running the getwd (get working directory) function without arguments. To change our working directory, we use setwd (set working directory) function and specify the path to the desired folder. We need to set working directory and path after cleaning up everything including your global environment of both r objects and loaded packages. Sometimes it is useful to Check r-version using print(version$version.string) function to be compatible for analysis to some extent.

rm(list=ls()) # Clean up everything 
# Set working directory 
setwd("F:/Maps") # setting path of working directory 
print(version$version.string) # Checking r-version

Required packages

As soon as local directory is set, we need to load required R packages such as tidyverse (Wickham, 2021), leaflet (Cheng, Karambelkar, et al., 2021), leafpop (Appelhans & Detsch, 2021), magrittr (Bache & Wickham, 2020), htmltools (Cheng, Sievert, et al., 2021), rjson (Couture-Beil, 2018), jsonlite (Ooms, 2020), reactable (Lin, 2020), htmlwidgets (Vaidyanathan et al., 2020), readr (Wickham & Hester, 2020), and glue (Hester, 2020).

# Loading packages 
library("tidyverse")
library("leaflet")
library("htmltools")
library("leafpop") 
library("magrittr")
library("rjson")
library("jsonlite")
library("reactable")
library("htmlwidgets")
library("dplyr")
library("glue")
library("sf")

Data

The next step is to load primary data, stored at local directory.

# Loading data from local directory  
mph_17th <- readr::read_csv("F:/Maps/mph_17th.csv")
## Creating popup labels
popup_labels <- glue::glue("<strong><span style='color:red'><span style='font-size: 25px'> Student Profile</strong></span><br/>
Student Name: <b><span style='color:blue'>{mph_17th$Name}<br/></span></b>
Country: <b><span style='color:orange'>{mph_17th$Country}<br/></span></b>
Last degree: <b><span style='color:green'>{mph_17th$Last_degree}<br/></span></b>
Category: <b><span style='color:deeppink'>{mph_17th$Category}<br/></span></b>
MPH Cohort: <b><span style='color:chartreuse'> {mph_17th$MPH_cohort} </span><br/></b>
Why are you here: <b><span style='color:blue'>{mph_17th$Why_here}<br/></span></b>
Why now: <b><span style='color:blue'>{mph_17th$Why_now}<br/></span><br/>") %>% 
    lapply(htmltools::HTML)
# use the default base map which is OpenStreetMap tiles
## Making a title of Map 
tag.map.title <- tags$style(HTML("
  .leaflet-control.map-title { 
    transform: translate(-50%,20%);
    position: fixed !important;
    left: 50%;
    text-align: center;
    padding-left: 10px; 
    padding-right: 10px; 
    background: rgba(255,255,255,0.75);
    font-weight: bold;
    font-size: 16px;
  }
"))

title <- tags$h3(
    tag.map.title, HTML("Master of Public Health (MPH) 17th Cohort, BRAC JPGSPH")
)  
mph_17th <- leaflet::leaflet() %>%
leaflet::addTiles() %>%  
#addProviderTiles(providers$Esri.WorldImagery, group = "World Imagery") %>%
#addProviderTiles(providers$Stamen.TonerLite, group = "Toner Lite") %>%
#addProviderTiles(providers$OpenStreetMap, group = "Open SM") %>%
addControl(title, position = "topleft", className="map-title") %>%
leaflet::addMarkers(lng=mph_17th$Longitude, lat=mph_17th$Latitude, clusterOptions = markerClusterOptions(),
                    popup = popup_labels, popupOptions = popupOptions(closeOnClick = FALSE, keepInView = TRUE,  closeButton = TRUE)) %>%
# setView(lng = 132.016716, lat = 38.130070, zoom = 1) %>%
    addMeasure(
        position = 'bottomleft',
        primaryLengthUnit = 'meters',
        primaryAreaUnit = 'sqmeters',
        activeColor = '#3D535D',
        completedColor = '#7D4479') %>%
addControl("<b><span style='color:blue'><span style='font-size: 14px'>To measure distance between JPGSPH located in Bangladesh and other countries click bottom leaft corner button</br>",
               position = 'bottomright')
    
# Saving map as html file 
htmlwidgets::saveWidget(widget = mph_17th,
                        file = "mph_17th_cohort.html",
                            selfcontained = TRUE)
mph_17th

Locations of Master of Public Health (MPH) Students, 17th Cohort (2021), BRAC JPGSPH

References

Appelhans, T., & Detsch, F. (2021). Leafpop: Include tables, images and graphs in leaflet pop-ups [Manual]. https://github.com/r-spatial/leafpop
Bache, S. M., & Wickham, H. (2020). Magrittr: A forward-pipe operator for r [Manual]. https://CRAN.R-project.org/package=magrittr
Cheng, J., Karambelkar, B., & Xie, Y. (2021). Leaflet: Create interactive web maps with the JavaScript leaflet library [Manual]. https://rstudio.github.io/leaflet/
Cheng, J., Sievert, C., Chang, W., Xie, Y., & Allen, J. (2021). Htmltools: Tools for HTML [Manual]. https://github.com/rstudio/htmltools
Couture-Beil, A. (2018). Rjson: JSON for r [Manual]. https://CRAN.R-project.org/package=rjson
Hester, J. (2020). Glue: Interpreted String Literals. R package version 1.4.2. [Manual]. https://CRAN.R-project.org/package=glue
Lin, G. (2020). Reactable: Interactive data tables based on react table [Manual]. https://CRAN.R-project.org/package=reactable
Ooms, J. (2020). Jsonlite: A simple and robust JSON parser and generator for r [Manual]. https://CRAN.R-project.org/package=jsonlite
Vaidyanathan, R., Xie, Y., Allaire, J., Cheng, J., Sievert, C., & Russell, K. (2020). Htmlwidgets: HTML widgets for r [Manual]. https://github.com/ramnathv/htmlwidgets
Wickham, H. (2021). Tidyverse: Easily install and load the tidyverse [Manual]. https://CRAN.R-project.org/package=tidyverse
Wickham, H., & Hester, J. (2020). Readr: Read rectangular text data [Manual]. https://CRAN.R-project.org/package=readr

  1. BRAC James P Grant School of Public Health, BRAC University, Dhaka-1213, Bangladesh, Corresponding Author’s Email:

    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License↩︎