Search code examples
javascriptrleafletrcharts

R variable in JavaScript


I am displaying a map via Leaflet with R using the package rCharts:
I added some polygons to the map and I want to get some information by clicking on them. So far I managed to get a Popup window to display the features name. I would also like to display some values from R variables, but I dont really know how to pass them over/include them to the JavaScript statement (See code.)

map <- Leaflet$new()
      map$geoJson((polygons),
    onEachFeature = "#! function(feature, layer){
    layer.bindPopup('<b>Name:</b>' + feature.properties.NAME + '<br> <b>Precipitation:</b> INSERT R VARIABLE HERE')
    } !#")
      map

Solution

  • I don't see why you can't just paste in the value to that character string. For example

    map <- Leaflet$new()
    map$geoJson((polygons),
    onEachFeature = paste("#! function(feature, layer){",
        "layer.bindPopup('<b>Name:</b>' + feature.properties.NAME + '<br> ",
        "<b>Precipitation:</b>", varibaleName, "')} !#", sep="")
    )
    map