andrewallenbruce / provider

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

Error: Mapping & `tidy = FALSE` #27

Closed andrewallenbruce closed 8 months ago

andrewallenbruce commented 8 months ago
library(provider)
purrr::map_dfr(util_years(), ~utilization(year = .x, 
                                          city = "Valdosta", 
                                          state = "GA", 
                                          type = "provider", 
                                          tidy = FALSE))
#> Error in `dplyr::bind_rows()`:
#> ! Can't combine `..1$Tot_HCPCS_Cds` <integer> and `..5$Tot_HCPCS_Cds` <character>.
#> Backtrace:
#>      ▆
#>   1. ├─purrr::map_dfr(...)
#>   2. │ └─dplyr::bind_rows(res, .id = .id)
#>   3. │   └─vctrs::vec_rbind(!!!dots, .names_to = .id, .error_call = current_env())
#>   4. └─vctrs (local) `<fn>`()
#>   5.   └─vctrs::vec_default_ptype2(...)
#>   6.     ├─base::withRestarts(...)
#>   7.     │ └─base (local) withOneRestart(expr, restarts[[1L]])
#>   8.     │   └─base (local) doWithOneRestart(return(expr), restart)
#>   9.     └─vctrs::stop_incompatible_type(...)
#>  10.       └─vctrs:::stop_incompatible(...)
#>  11.         └─vctrs:::stop_vctrs(...)
#>  12.           └─rlang::abort(message, class = c(class, "vctrs_error"), ..., call = call)

Created on 2023-10-27 with reprex v2.0.2

andrewallenbruce commented 8 months ago

Fix: pipe the response into the below one-liner:

results <- httr2::resp_body_json(response, simplifyVector = TRUE) |>
    dplyr::mutate(dplyr::across(dplyr::everything(), as.character))

Edit: created helper function df2chr():

results <- httr2::resp_body_json(response, simplifyVector = TRUE) |>
    df2chr()

Edit: should be of the form:

results <- httr2::resp_body_json(response, simplifyVector = TRUE)

  if (!tidy) results <- df2chr(results)

  if (tidy) {...}
andrewallenbruce commented 8 months ago

Add to: