fetch_provider_data: Fetch Provider Data as Tibble or Download CSV

View source: R/dl-provider-data.R

fetch_provider_dataR Documentation

Fetch Provider Data as Tibble or Download CSV

Description

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.

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.

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.

See Also

get_provider_meta_data

Examples

library(dplyr)

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

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

df_tbl |>
 glimpse()


healthyR.data documentation built on July 4, 2024, 5:07 p.m.