Leaflet map with subclass of TileLayer

OK, I figured this out, it is possible using htmlwidgets::onRender.

I'm using a variation of this TileLayer subclass. The function to create my custom tile layer is L.TileLayer.xhr. The renderLeaflet code looks roughly like this:

output$map = renderLeaflet({
    map = leaflet() # Don't addTiles here!
    url_template = 'http://...' # XYZ tile URL
    auth_header = '...' # Authorization token to add to headers
    js_code = stringr::str_glue("
    function(el, x, data) {{
        var xhrLayer = L.TileLayer.xhr(
            '{url_template}', data.options, data.headers)
        .addTo(this);
    }}")
    map = htmlwidgets::onRender(map, js_code, data=list(
        headers=list(list(header='Authorization', value=auth_header))))
    map
})
3 Likes