# NOT RUN {
library(leaflet)
# This example uses browser geolocation. RStudio users:
# this won't work in the Viewer pane; try popping it
# out into your system web browser.
leaflet() %>% addTiles() %>%
onRender("
function(el, x) {
// Navigate the map to the user's location
this.locate({setView: true});
}
")
# This example shows how you can make an R data frame available
# to your JavaScript code.
meh <- "😐";
yikes <- "😨";
df <- data.frame(
lng = quakes$long,
lat = quakes$lat,
html = ifelse(quakes$mag < 5.5, meh, yikes),
stringsAsFactors = FALSE
)
leaflet() %>% addTiles() %>%
fitBounds(min(df$lng), min(df$lat), max(df$lng), max(df$lat)) %>%
onRender("
function(el, x, data) {
for (var i = 0; i < data.lng.length; i++) {
var icon = L.divIcon({className: '', html: data.html[i]});
L.marker([data.lat[i], data.lng[i]], {icon: icon}).addTo(this);
}
}
", data = df)
# }
# NOT RUN {
# }
Run the code above in your browser using DataLab