Search code examples
rrstudior-leaflet

LeafletR map not showing in browser


I'm looking for guidance on getting a basic example of leafletR package to show a map in the browser. Working with the most basic example, the map doesn't show up in the browser. I've checked the .html and .json files, which are present and appear to properly formatted. Javascript inspector shows this error:

XMLHttpRequest cannot load file:///var/folders/_0/tdgf_k7x77qdn64_gk9ynljm0000gp/T/Rtmp6O4QZM/Fiji_Earthquakes/quakes.geojson. Received an invalid response. Origin 'null' is therefore not allowed access.

I'm on a Mac: OSX 10.7.5 RStudio Version: 0.98.857

Code

library(leafletR)


data(quakes)

# store data in GeoJSON file (just a subset here)
q.dat <- toGeoJSON(data=quakes[1:99,], dest=tempdir(), name="quakes")

# make style based on quake magnitude
q.style <- styleGrad(prop="mag", breaks=seq(4, 6.5, by=0.5),     style.val=rev(heat.colors(5)), leg="Richter Magnitude", fill.alpha=0.7, rad=8)

# create map
q.map <- leaflet(data=q.dat, dest=tempdir(), title="Fiji Earthquakes", base.map="osm", style=q.style, popup="mag")

# view map in browser
browseURL(q.map)

Solution

  • A quick solution to open the map using a local web server is to take advantage of RStudio's HTML viewer.

    rstudio::viewer(qmap)