walkerke / mapgl

R interface to Mapbox GL JS v3 and Maplibre GL JS
https://walker-data.com/mapgl
Other
87 stars 5 forks source link

Shinyapp using mapgl/maplibre crashes when deployed #54

Open kmcd39 opened 4 days ago

kmcd39 commented 4 days ago

Hi!

Deploying the shiny app i was developing using mapgl and maplibre and carto, as created with:

mapgl::maplibre(
     style = carto_style("dark-matter")
    ,preserveDrawingBuffer = TRUE # per GH issue, important for capture::capture
  )

However, when I deploy the app, it loads initially -- the map even renders! However, the app crashes as the map is drawn and I get the "Disconnected from server" message.

The app works locally, and logs from shinyapps.io show nothing unusual and no errors. However, when i inspect the page on either chrome or firefox, I get errors from maplibregl.js, including:

"Source map error: Error: request failed with status 404"

And--

TypeError: HTMLWidgets.find(...) is undefined
    <anonymous> [shinyapp_url]/_w_cd3d9e1d/maplibregl-binding-0.1.4/maplibregl.js:933

I've also tried this with json deployed with the app and read with jsonlite but got the same errors. I'll also include these other errors from chrome (i'm sorry, i'm not experienced debugging html/javascript so not sure what is relevant)--

image

Thank you!

walkerke commented 4 days ago

I've done a little testing and I think I know what's going on. In a Shiny session when proxies (e.g. maplibre_proxy()) are used, I use a getMap() function to retrieve the map instance so we can operate on it in a proxy session. However, we're seeing the proxy trying to operate before the map is loaded in some cases which is triggering the error.

I'm only seeing this with data-intensive apps where the initial map load takes some time, I'm not seeing it for faster-loading apps. Would this characterize your app?

I think a solution would be to ensure the proxy session waits until a map exists before it tries to do anything, if so.

kmcd39 commented 4 days ago

This makes sense...

It seems to square with some maplibre (outside of R/shiny) discussion I saw too, and my app is more data intensive.

Is there a way to check on the existence of the map? And then i could use a req statement in the shiny observers to prevent operations on the proxy before it exists.

kmcd39 commented 2 days ago

Using req(proxy) wasn't enough to fix it, but there is now a version that is working while deployed that uses a fraction of the data. So it does seem to confirm the initial render with a lot of data was the operative issue.

Please let me know if you think this is something resolvable, if it's something you're working on, or if you think there are avenues to address this issue within the app itself. I'm also working with the client to upgrade the memory on the hosting server, although I think that should show up in the logs if it were the issue.

Thank you!