render leaflet plots as raster in R?
<p>I realize that this largely defeats the purpose of using an interactive <code>leaflet</code> map, but I'm writing a traditional paper book and I want to show how the <code>leaflet</code> package for R works. I'm writing the book in LaTeX and rendering with <code>knitr</code>. Is there a way to have a <code>leaflet</code> map render as a raster image such that it could be included in this book? </p>

<p>Here is a minimal example:</p>

<pre><code>library(leaflet)
map &lt;- leaflet() %&gt;%
  addTiles() %&gt;%
  addMarkers(lng = -77.03673, lat = 38.89761)
</code></pre>

<p>Now if I try a chunk like:</p>

<pre><code>&lt;&lt;&gt;&gt;=
map
@
</code></pre>

<p>I get this error:</p>

<pre><code>Error in validateCssUnit(sizeInfo$width): "\maxwidth" is not a valid CSS unit
(e.g., "100%", "400px", "auto")
</code></pre>

<p>Trying to save as a PNG doesn't work either:</p>

<pre><code>&lt;&lt;&gt;&gt;=
png(filename = "test.png")
map
dev.off()
@
</code></pre>

<p><code>map</code> doesn't inherit from <code>ggplot</code>, so <code>ggsave</code> won't work either. </p>

<p>Is there any way to make this work? </p>