Rmarkdown with leaflet/tmaps fails in pkgdown::build_articles() but renders with rmarkdown::render()

38 Views Asked by At

Example rmarkdown file here and datafile here.

Code is quite simple:

linestring <- readRDS(gzcon(url("https://github.com/marine-ecologist/testpkg/raw/main/data/particletracks189.rds")))

> head(linestring)
Simple feature collection with 6 features and 1 field
Geometry type: MULTILINESTRING
Dimension:     XY
Bounding box:  xmin: 1630608 ymin: 8351189 xmax: 1633947 ymax: 8354471
Projected CRS: AGD84 / AMG zone 53
# A tibble: 6 × 2
  id                                                                                 geometry
  <chr>                                                                 <MULTILINESTRING [m]>
1 EVI0   ((1630627 8353937, 1630625 8353934, 1630623 8353932, 1630621 8353930, 1630619 835...
2 EVI1   ((1630627 8353937, 1630628 8353942, 1630630 8353947, 1630632 8353952, 1630634 835...
3 EVI10  ((1630627 8353937, 1630626 8353936, 1630625 8353936, 1630624 8353936, 1630624 835...
4 EVI100 ((1630627 8353937, 1630627 8353937, 1630626 8353938, 1630626 8353938, 1630626 835...
5 EVI101 ((1630627 8353937, 1630627 8353936, 1630628 8353935, 1630628 8353934, 1630629 835...
6 EVI102 ((1630627 8353937, 1630627 8353936, 1630626 8353935, 1630626 8353934, 1630626 835...



linestring_subset <- linestring |> slice(1:200)

tmap_mode("view") + 
  tm_shape(linestring_subset) + 
  tm_lines()

This will work with pkgdown::build_articles() fine with a subset (1:200) of the 1k linestrings, but fails with the full 1k dataset:

linestring <- readRDS(gzcon(url("https://github.com/marine-ecologist/testpkg/raw/main/data/particletracks189.rds")))

tmap_mode("view") + 
  tm_shape(linestring_subset) + 
  tm_lines()

The code runs in tmap and renders with rmarkdown::render(), but I'm uncertain as to why it fails with pkgdown::build_articles().

The issue is intermittent and solely seems to come from linestrings, but doesn't seem to be related to the number of linestrings or whether the sf geometry is valid (st_is_valid() returns TRUE consistently).

I've also tried converting tmap to leaflet using tmpplot_leaflet <- tmap_leaflet(tmpplot, mode = "view") and plotting the whole map in leaflet:

leaflet()  %>% addTiles() %>%
addMarkers(lng = 145.4498,lat= -14.64987,popup="Hi there") |>
addPolylines(data = linestring_subset |> st_transform(4326))

But the issue is still the same in that the html file won't render beyond the failed map.

Lastly, the leaflet div (and data) does seem to be there in the html:

<div class="leaflet html-widget html-fill-item-overflow-hidden html-fill-item" id="htmlwidget-a9113457d334d2ed7373" style="width:700px;height:432.632880098888px;"></div>
<script type="application/json" data-for="htmlwidget"

But beyond that I'm not sure why it's not functioning. Is this something to do with htmltools and pkgdown css compatability? Any leads on resolving it (and why it works in render not build_articles?

edit: the code seems to render fine via leaflet & shiny as well, which makes me suspect this is a specific pkgdown issue with htmltools


    library(shiny)
    library(leaflet)
    
    ui <- fluidPage(
      titlePanel("Leaflet in Shiny"),
      sidebarLayout(
        sidebarPanel(
          # controls
        ),
        mainPanel(
          leafletOutput("map")
        )
      )
    )
    
    server <- function(input, output, session) {
      output$map <- renderLeaflet({
        leaflet() %>% addTiles() %>%
          addPolylines(data = linestring |> st_transform(4326))
      })
    }
    
    shinyApp(ui, server)

0

There are 0 best solutions below