andrewallenbruce / provider

Public Healthcare Provider APIs :stethoscope:
https://andrewallenbruce.github.io/provider/
Other
18 stars 2 forks source link

Bug: `change()` errors with dates #85

Closed andrewallenbruce closed 1 month ago

andrewallenbruce commented 1 month ago
library(tidyverse)
library(provider)
library(clock)

# Fine
provider:::gen_data(2020:2025) |> 
  provider::change(pay)
#> # A tibble: 12 × 6
#>     year group   pay pay_chg  pay_pct pay_ror
#>    <int> <chr> <int>   <int>    <dbl>   <dbl>
#>  1  2020 A      1576       0  0         1    
#>  2  2021 A      1204    -372 -0.236     0.764
#>  3  2022 A      1192     -12 -0.00997   0.990
...

# Errors on dates
tibble(
  date = c(
    date_build(2023, 12), 
    date_build(2024, 1:5)
  ),
  total = c(
    1132783.09,
    942482.25,
    949739.89,
    985444.69,
    888797.41,
    808376.95
  )) |> 
  provider::change(total)
#> Error in `dplyr::mutate()`:
#> ℹ In argument: `dplyr::across(...)`.
#> Caused by error in `across()`:
#> ! Can't compute column `date`.
#> Caused by error in `Math.Date()`:
#> ! sign not defined for "Date" objects

# <date> is a double
is.double(date_build(2023, 12))
#> [1] TRUE

# but is not numeric
is.numeric(date_build(2023, 12))
#> [1] FALSE

Error is on line 88, here:

dplyr::mutate(
   dplyr::across(
   dplyr::where(is.double), <-- Here
    ~janitor::round_half_up(., digits = digits)))

Created on 2024-05-10 with reprex v2.1.0

andrewallenbruce commented 1 month ago

Removed the call to janitor::round_half_up(); rounding at point of data retrieval questionable anyway:

library(tidyverse)
library(clock)
library(provider)

tibble(
  date = c(
    date_build(2023, 12), 
    date_build(2024, 1:5)
  ),
  total = c(
    1132783.09,
    942482.25,
    949739.89,
    985444.69,
    888797.41,
    808376.95
  )) |> 
  provider::change(total)
#> # A tibble: 6 × 5
#>   date          total total_chg total_pct total_ror
#>   <date>        <dbl>     <dbl>     <dbl>     <dbl>
#> 1 2023-12-01 1132783.        0    0           1    
#> 2 2024-01-01  942482.  -190301.  -0.168       0.832
#> 3 2024-02-01  949740.     7258.   0.00770     1.01 
#> 4 2024-03-01  985445.    35705.   0.0376      1.04 
#> 5 2024-04-01  888797.   -96647.  -0.0981      0.902
#> 6 2024-05-01  808377.   -80420.  -0.0905      0.910

Created on 2024-05-13 with reprex v2.1.0