rknitrr-mapview

mapview error in knitr R markdown document


I've been playing around with the mapview package which has recently been loaded onto CRAN. I've been playing with the demo code and trying to embed it into a knitr markdown document. Unfortunately, I get an error message when I do this and I'm having trouble interpreting what is going wrong.

Here is the error message, followed by a reproducible example. Note that I don't have any problem when running the code as straight-up R code. It's only when it is run through knitr that the problem presents itself. The error does not occur if the mapview elements are commented out.

I've updated all of my packages and the problem persists. Here are some system details:

pandoc.exe: Could not fetch C:\Users\my.name\Documents\R\win-library\3.2\mapview\htmlwidgets\lib\leaflet#default#VML C:\Users\my.name\Documents\R\win-library\3.2\mapview\htmlwidgets\lib\leaflet: openBinaryFile: does not exist (No such file or directory) Error: pandoc document conversion failed with error 67 In addition: Warning message: running command '"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS Mapview.utf8.md --to html --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash-implicit_figures --output Mapview.html --smart --email-obfuscation none --self-contained --standalone --section-divs --template "C:\Users\my.name\Documents\R\win-library\3.2\rmarkdown\rmd\h\default.html" --variable "theme:bootstrap" --include-in-header "C:\Users\my.name\AppData\Local\Temp\Rtmpw9Mi9D\rmarkdown-str1ee41c515f3f.html" --mathjax --variable "mathjax-url:https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" --no-highlight --variable "highlightjs=C:\Users\my.name\Documents\R\win-library\3.2\rmarkdown\rmd\h\highlight"' had status 67 Execution halted

---
title: "Test Mapview"
output: html_document
---


```{r}

library(ggplot2)
library(rgdal)
library(mapview)

data(diamonds)

### blow diamonds up a bit
big <- diamonds[rep(seq_len(nrow(diamonds)), 1), ]
big$cut <- as.character(big$cut)
big$color <- as.character(big$color)
big$clarity <- as.character(big$clarity)

### provide some random positions
big$x <- rnorm(nrow(big), 0, 10)
big$y <- rnorm(nrow(big), 0, 10)
coordinates(big) <- ~x+y
proj4string(big) <- CRS("+init=epsg:4326")

### view it
mapview(big)

```

Solution

  • There is, so far, no support for knitr in mapview as such. That said, it is possible to embed "small" data sets via an explicit call to mapview(x)@map. With x being a Spatial* object with less than 30000 features for polygons and lines and less than 20000 features for points. These numbers are the default limits set in mapviewOptions() for rendering output using the leaflet package. For data sets with more features than those thresholds we use specialty functions mapview:::fpView() for points and mapview:::bView() for polygons and lines. For these specialty functions no knitr support exists at the moment.

    This means that your only option for embedding larger data sets with knitr is to set the thresholds for "maxlines", "maxpoints" or "maxpolygons" to an appropriate number (higher than the number features) so that rendering is done using the leaflet package functions. But you need to keep in mind that leaflet is likely to not behave smoothly anymore with large data sets (or crash completely).

    So, for the above example:

    ---
    title: "Test Mapview"
    output: html_document
    ---
    
    
    ```{r}
    
    library(ggplot2)
    library(rgdal)
    library(mapview)
    
    mapviewOptions(maxpoints = 55000) # diamonds has some 53000 rows
    
    
    data(diamonds)
    
    ### blow diamonds up a bit
    big <- diamonds[rep(seq_len(nrow(diamonds)), 1), ]
    big$cut <- as.character(big$cut)
    big$color <- as.character(big$color)
    big$clarity <- as.character(big$clarity)
    
    ### provide some random positions
    big$x <- rnorm(nrow(big), 0, 10)
    big$y <- rnorm(nrow(big), 0, 10)
    coordinates(big) <- ~x+y
    proj4string(big) <- CRS("+init=epsg:4326")
    
    ### view it
    mapview(big)@map
    
    ```
    

    should produce a document that shows the map with points, but as I said, be aware that this may well be beyond the capabilities of the leaflet package.

    In addition, I would like to raise another point here. It is more than questionable whether knitr is an appropriate way to create an html file for such big data sets. knitr stores everything (geometries and attributes) in one html file which will likely get very big very quickly and become rather unresponsive.

    Therefore, maybe a shiny solution is a better option for such big data.

    So far, our focus was on getting the support for big data sets working correctly in the viewer/browser. knitr integration will hopefully be implemented at some stage, but it may take some time to get this done correctly.

    Hope this clarifies, Best Tim