Introduction
Here is the final script from the end of the tutorial video above. Following this will be a demonstration of including hyperlinks in your popup windows.
# load leaflet library
library(leaflet)
# Set up your data frame
data <- data.frame(c(43.11940,43.11940),
c(-79.24658,-79.244658),
c("HQ 1","HQ 2"),
c(4736583,3204853),
c('',
''))
# give names to your data columns
names(data) <- c("lat","lng","name","score","video")
# create the leaflet map variable
m <- leaflet() %>%
addProviderTiles("Esri.WorldTopoMap") %>%
addMarkers(data=data,
lat=~lat,
lng=~lng,
popup= ~paste("Hello World
","Name:",name,"
","Score:",score, video, sep=" ")
)
# print map
m
Adding Hyperlinks
There are plenty of ways to go about doing this in the leaflet package, but I will outline two here.
- A single static link for in all popup windows
- Creating dynamic variables to use for seperate links in each popup window.
Option 1
- Here we will simply add a new line to the paste function in the origional script above. HTML links can br written using the following notation:
<a href="url">link text</a>
- To impliment this into the
paste()
function inR
we will use a link to the GeomaTECHs YouTube channel: https://www.youtube.com/channel/UCTalI0S14Ek6DcvvvFIFPOg.
# create the leaflet map variable
m <- leaflet() %>%
addProviderTiles("Esri.WorldTopoMap") %>%
addMarkers(data=data,
lat=~lat,
lng=~lng,
popup= ~paste("Hello World
","Name:",name,"
","Score:",score, video,
"
","Link:","GeomaTECHs YT Channel",sep=" ")
)
# print map
m
Option 2
- In this option we will add a different hyperlink to each popup by adding the links into the
data
variable that was created.First create a list of the links you would like to add to your data frame. This step will only need to be done if you are creating your data from scratch or if you need to combine an existing dataset with a set of hyperlinks. If it is the latter, then be sure the list of hyperlinks is as long as the length of the columns in your origional data or you may have problems.
Okay lets create the list of hyperlinks:
# create list of links in a variable links.list <- c("GeomaTECHs YT Channel", "GeomaTECHs Tutorial")
Now we can add a
link
colummn to ourdata
variable using thedata$link
.data$link <- links.list
The final step is to add the variable
link
to thepaste()
function. In this example I have placed the link under the YouTube videos.# create the leaflet map variable m <- leaflet() %>% addProviderTiles("Esri.WorldTopoMap") %>% addMarkers( data=data, lat=~lat, lng=~lng, popup= ~paste("
Hello World
","Name:",name,"
","Score:",score, video,link,sep=" ") ) # print the map m
Conclusion
For every tutorial video on YouTube I will try and use this Steemit account to expand on any complicated questions that viewers may have. As more questions come in I will add information to these documents hopefully answering them. So be sure to keep checking back for new information.