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

realtime_ws() server side daily averages to speed up download for Shiny App #199

Closed
bevingtona opened this issue Jul 28, 2023 · 1 comment

Comments

@bevingtona
Copy link

I'm working on a Shiny App that displays realtime data. The realtime_ws() function in the httr2 branch is fabulous.

In this context, I am only interested in daily averages. The app is a little slow because the realtime_ws() functions downloads 5 minute data. Is there a way to get daily data from the server and not require downloading the 5 min data, then calculate daily averages?

(I really don't know the mechanics of the realtime_ws() function, so no idea if server side calculations are doable!)

library(tidyhydat)
library(dplyr)

# Works, but I do not require 5 minute data ##########################################

rt <- tidyhydat::realtime_ws("07EC003",
                             parameter = 47,
                             start_date = as.Date("2023-01-01"),
                             end_date = Sys.Date())
#> All station successfully retrieved
#> All parameters successfully retrieved
print(head(rt))
#> # A tibble: 6 × 10
#>   STATION_NUMBER Date                Name_En   Value Unit  Grade Symbol Approval
#>   <chr>          <dttm>              <chr>     <dbl> <chr> <chr> <chr>  <chr>   
#> 1 07EC003        2023-01-01 00:00:00 Discharg…  6.14 m3/s  10    <NA>   Final/F…
#> 2 07EC003        2023-01-01 00:05:00 Discharg…  6.14 m3/s  10    <NA>   Final/F…
#> 3 07EC003        2023-01-01 00:10:00 Discharg…  6.14 m3/s  10    <NA>   Final/F…
#> 4 07EC003        2023-01-01 00:15:00 Discharg…  6.14 m3/s  10    <NA>   Final/F…
#> 5 07EC003        2023-01-01 00:20:00 Discharg…  6.14 m3/s  10    <NA>   Final/F…
#> 6 07EC003        2023-01-01 00:25:00 Discharg…  6.14 m3/s  10    <NA>   Final/F…
#> # ℹ 2 more variables: Parameter <dbl>, Code <chr>

# Does not work ##########################################
rt_daily <- rt %>% 
  tidyhydat::realtime_daily_mean(na.rm = T)
#> Error in `dplyr::group_by()`:
#> ! Must group by variables found in `.data`.
#> ✖ Column `PROV_TERR_STATE_LOC` is not found.
#> Backtrace:
#>     ▆
#>  1. ├─rt %>% tidyhydat::realtime_daily_mean(na.rm = T)
#>  2. └─tidyhydat::realtime_daily_mean(., na.rm = T)
#>  3.   ├─dplyr::group_by(...)
#>  4.   └─dplyr:::group_by.data.frame(df_mean, STATION_NUMBER, PROV_TERR_STATE_LOC, Date, Parameter)
#>  5.     └─dplyr::group_by_prepare(.data, ..., .add = .add, error_call = current_env())
#>  6.       └─rlang::abort(bullets, call = error_call)

# My work arround ##########################################
rt_daily <- rt %>% 
  mutate(Date = as.Date(Date)) %>% 
  group_by(Date) %>% 
  summarise(Value = mean(Value, na.rm = T))

print(head(rt_daily))
#> # A tibble: 6 × 2
#>   Date       Value
#>   <date>     <dbl>
#> 1 2023-01-01  6.10
#> 2 2023-01-02  6.04
#> 3 2023-01-03  5.99
#> 4 2023-01-04  5.95
#> 5 2023-01-05  5.90
#> 6 2023-01-06  5.85

Created on 2023-07-28 with reprex v2.0.2

@boshek
Copy link
Collaborator

boshek commented Jul 28, 2023

The realtime_ws() function in the httr2 branch is fabulous.

So that should be merged and also up on CRAN now.

But alas, no server side calculations with the webservice though that sure would be useful. Filing a request with ECCC would probably be useful.

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

2 participants