1

I am trying to display a map with R using the rCharts package. I am starting simple and so I want to add a polygon to my map. But I have no clue how. Any ideas? addPolygon doesnt work.

map <- Leaflet$new()


map$tileLayer(provider = 'Stamen.TonerLite')

map$setView(c(48.1, 16.7), zoom = 10)
map$addPolygon(
  c(48.99831, 49.08815, 49.08815, 48.99831, 48.99831),
  c(13.42666, 13.42666, 13.56383, 13.56358, 13.42666),
  layerId=c("1"),
  options=opts,
  defaultOptions=opts)
map
Spacedman
  • 92,590
  • 12
  • 140
  • 224
maRtin
  • 6,336
  • 11
  • 43
  • 66
  • 1
    "Doesn't work" how? Error message? Don't see it on the map? Is it there in the code? Do you mean the `leafletR` package? Or something else not from CRAN? Where? What version? – Spacedman May 08 '14 at 16:05
  • I am using the "rCharts" package and I get the following error message: Error in envRefInferField(x, what, getClass(class(x)), selfEnv) : ‘addPolygon’ is not a valid field or method name for reference class “Leaflet” – maRtin May 08 '14 at 16:19
  • What makes you think "addPolygon" would do anything? Did you read it somewhere? There's no such thing in the package... – Spacedman May 08 '14 at 16:32
  • you are right! Do you have any idea what function i could use to add a polygon to my map? Would it maybe be easier to do that with the leaftletR package? My problem witht the leafletR package is, that I dont know how to implement the map in shiny once I made. – maRtin May 08 '14 at 16:43

1 Answers1

6

Add the polygon to your map by converting to geoJSON format, as in example 10 in the rCharts source code: https://github.com/ramnathv/rCharts/blob/master/inst/libraries/leaflet/examples/example10.R

Note how lat and long are different between the xy coords in the geoJSON and the setView. This here code gives me a blue box in the Czech Republic close to Germany.

xy = cbind(
  c(13.42666, 13.42666, 13.56383, 13.56358, 13.42666),
    c(48.99831, 49.08815, 49.08815, 48.99831, 48.99831)
    )

xyjson = RJSONIO::toJSON(xy)

jsonX = paste(
    '{"type":"FeatureCollection","features":[
        {"type":"Feature",
         "properties":{"region_id":1, "region_name":"My Region"},
         "geometry":{"type":"Polygon","coordinates": [ ',xyjson,' ]}}
       ]
      }')

polys = RJSONIO::fromJSON(jsonX)
map = Leaflet$new()
map$tileLayer(provider = 'Stamen.TonerLite')
map$setView(c(49.1,13.5), zoom = 8)
map$geoJson(polys)
map
# or print(map) from a script probably.

If you have more than one polygon, you need to create several structures of the {"type": "Feature", and comma-separate them within the square brackets of the "features" of the "FeatureCollection". I've re-indented things a bit to show the structure better. Its getting to the point where a templating system like the brew package is going to help you...

Spacedman
  • 92,590
  • 12
  • 140
  • 224
  • Newbie question, but how then can I draw TWO polygons? (I'm actually going to be drawing hundreds, would love to be able add labels/popups/colours to the polygons too) – wizard_draziw Oct 09 '14 at 23:39
  • I suspect it might be easier to ask the package authors to add some functionality since this is pretty common usage - but I've edited a bit to show how to do it in principle. – Spacedman Oct 10 '14 at 08:23