fetch_cms_data: Fetch Data as Tibble

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

fetch_cms_dataR Documentation

Fetch Data as Tibble

Description

This function retrieves data from the provided link and returns it as a tibble with cleaned names. This function is intended to be used with the CMS data API function get_cms_meta_data.

Usage

fetch_cms_data(.data_link)

Arguments

.data_link

A character string containing the URL to fetch data from.

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. 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.

Value

A tibble containing the fetched data with cleaned names. If an error occurs, returns NULL.

See Also

get_cms_meta_data

Examples

library(dplyr)

# Example usage:
base_url <- "https://data.cms.gov/data-api/v1/dataset/"
data_identifier <- "9767cb68-8ea9-4f0b-8179-9431abc89f11"
data_url <- paste0(base_url, data_identifier, "/data")

df_tbl <- fetch_cms_data(data_url)

df_tbl |>
 head(1) |>
 glimpse()


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