Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem reading file with some leaflet content #455

Open
cderv opened this issue Mar 14, 2025 · 0 comments
Open

Problem reading file with some leaflet content #455

cderv opened this issue Mar 14, 2025 · 0 comments

Comments

@cderv
Copy link

cderv commented Mar 14, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant