tpisel / openmeteo

Open Meteo SDK for R
13 stars 2 forks source link

Openmeteo doesn't work with Shinylive ? #9

Open weberam2 opened 1 month ago

weberam2 commented 1 month ago

Sorry if I am out of my depths here.

I built a ShinyR app that populates a map with Lon/Lat values. And I had it such that if you click on one of the icons, it would call openmeteo, download the forecast, then plot it using ggplot

The app works as expect; but when I convert it to an HTML using shinylive shinylive::export(appdir = "../../ShinyRMap", destdir = "docs") the HTML version doesn't work httpuv::runStaticServer("docs")

I get two warning messages when I run shinylive:

Warning messages: 1: In find.package(pkgs, lib.loc = NULL, quiet = FALSE, !is_quiet()) : package ‘scales’ found more than once, using the first from “/home/weberam2/R/x86_64-pc-linux-gnu-library/4.4/scales”, “/usr/lib/R/site-library/scales” 2: Can't find curl in webR binary repository.

I'm guessing it's the curl issue?

I'm happy to share the full code if that helps.

This is how I'm calling openmeteo:

weatherdata <- openmeteo::weather_forecast(
      c(click$lat, click$lng),
      daily = c(
        "temperature_2m_max",
        "precipitation_sum",
        "windspeed_10m_max"
      )
    )
tpisel commented 1 month ago

Hey, I looked around a little. openmeteo uses httr to do the API calls, which uses curl. I haven't used shinylive myself but per this comment there's more fundamental limitations on opening websockets in the WebAssembly sandbox. It might be possible to use the direct approach they mention, i.e.

query_url <- paste0(
  'https://api.open-meteo.com/v1/forecast?latitude=',
  click$lat,
  '&longitude=',
  click$lng,
  '&daily=temperature_2m_max,precipitation_sum,wind_speed_10m_max'
)

response <- readLines(query_url, warn=FALSE) |> jsonlite::fromJSON()

If that works in shinylive I could look at tweaking the httr calls to use the base methods instead -- the comment above also mentioned possible CORS issues.

weberam2 commented 1 month ago

Hi Tom,

Thanks for getting back to me. I tried your solution but no luck.

I simplified my code to figure out where the problem lies, and it seems like it is indeed readLines

So frustrating. I'll see if I can find any other workarounds...

weberam2 commented 1 month ago

Nevermind! That does work!

I must have introduced another bug somewhere, but I fixed it and your solution worked :smile: