Skip to content

Problem reading file with some leaflet content #455

@cderv

Description

@cderv

This comes from investigating

The problem seems to be that xml2 doesn't parse after the leaflet script part.

Here is a reprex without bookdown*

## load libraries
library(leaflet)

## open services
boem_open = arcgislayers::arc_open('https://services7.arcgis.com/G5Ma95RzqJRPKsWL/arcgis/rest/services/Wind_Lease_Boundaries__BOEM_/FeatureServer/8')

## active lease areas
active_leases = arcgislayers::arc_select(boem_open) |>
  dplyr::filter(substr(LEASE_NUMBER, 1, 5) == 'OCS-A') |>
  sf::st_transform(4326) |>
  sf::st_make_valid()

map = leaflet() |>
  addProviderTiles(providers$Esri.WorldTopoMap) |>
  addPolygons(data = active_leases, fillColor = 'transparent', color = 'red', opacity = 1, weight = 2) |> 
  addPolygons(data = active_leases, fillColor = 'transparent', color = 'red', opacity = 1, weight = 2) 

path <- tempfile(pattern = "bug", fileext = ".html")
htmlwidgets::saveWidget(map, path)
#> Registered S3 method overwritten by 'jsonlite':
#>   method     from   
#>   print.json jsonify

library(xml2)

html <- read_html(path)

# 3 last lines of the file
res <- readr::read_lines_raw(path)
vapply(res[tail(seq_along(res), 3)], rawToChar, "html content")
#> [1] "<script type=\"application/htmlwidget-sizing\" data-for=\"htmlwidget-dc2ea44e59569184d7e4\">{\"viewer\":{\"width\":\"100%\",\"height\":400,\"padding\":0,\"fill\":true},\"browser\":{\"width\":\"100%\",\"height\":400,\"padding\":0,\"fill\":true}}</script>"
#> [2] "</body>"                                                                                                                                                                                                                                                      
#> [3] "</html>"

# they can't be seen by xml2
xml2::xml_find_first(html, "//body") |> xml2::html_structure()
#> <body [style]>
#>   {text}
#>   <div#htmlwidget_container>
#>     {text}
#>     <div#htmlwidget-dc2ea44e59569184d7e4 .leaflet.html-widget.html-fill-item [style]>
#>     {text}
#>   {text}
#>   <script [type, data-for]>
#>     {cdata}

# script tag after the leaflet is not found 
xml2::xml_find_all(html, "//body//script")
#> {xml_nodeset (1)}
#> [1] <script type="application/json" data-for="htmlwidget-dc2ea44e59569184d7e4 ...

Created on 2025-03-14 with reprex v2.1.1

The script type="application/htmlwidget-sizing" element is not found.

This is the issue with bookdown processing as xml2 is used for post processing the html after pandoc.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions