Skip to contents

This function retrieves provider data from the provided link and returns it as a tibble with cleaned names or downloads the data as a CSV file if the link ends in .csv. This function is intended to be used with the CMS provider data API.

Usage

fetch_provider_data(.data_link, .limit = 500)

Arguments

.data_link

A character string containing the URL to fetch data from.

.limit

An integer specifying the maximum number of rows to fetch. Default is 500. If set to 0, all records will be returned.

Value

A tibble containing the fetched data with cleaned names, or downloads a CSV file to the user-selected directory. If an error occurs, returns NULL.

Details

The function sends a request to the provided URL using httr2::request and httr2::req_perform. If the response status is not 200, it stops with an error message indicating the failure. If the URL ends in .csv, it uses utils::download.file to download the CSV file to a directory chosen by the user. Otherwise, the response body is parsed as JSON and converted into a tibble using dplyr::as_tibble. The column names are cleaned using janitor::clean_names, and any character columns are stripped of leading and trailing whitespace using stringr::str_squish. The default limit for a return on records is 500. If the limit is set to 0, all records will be returned.

Examples

library(dplyr)

# Example usage:
data_url <- "069d-826b"

df_tbl <- fetch_provider_data(data_url, .limit = 1)

df_tbl |>
 glimpse()
#> Rows: 1
#> Columns: 15
#> $ zip_code                                             <chr> "00210"
#> $ min_medicare_pricing_for_new_patient                 <chr> "60.136"
#> $ max_medicare_pricing_for_new_patient                 <chr> "181.552"
#> $ mode_medicare_pricing_for_new_patient                <chr> "137.576"
#> $ min_copay_for_new_patient                            <chr> "15.034"
#> $ max_copay_for_new_patient                            <chr> "45.388"
#> $ mode_copay_for_new_patient                           <chr> "34.394"
#> $ most_utilized_procedure_code_for_new_patient         <chr> "99204"
#> $ min_medicare_pricing_for_established_patient         <chr> "18.896"
#> $ max_medicare_pricing_for_established_patient         <chr> "148.44"
#> $ mode_medicare_pricing_for_established_patient        <chr> "75.064"
#> $ min_copay_for_established_patient                    <chr> "4.724"
#> $ max_copay_for_established_patient                    <chr> "37.11"
#> $ mode_copay_for_established_patient                   <chr> "18.766"
#> $ most_utilized_procedure_code_for_established_patient <chr> "99213"