You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I was working on a separate project that relied on the FFS data. It might be interesting to include this data source within this project and compare it against the managed care provider data.
library(dplyr)
library(fs)
library(glue)
library(httr2)
library(readr)
library(stringr)
# Helper function to derive the current month's file name from the response urlget_file_name_from_url<-function(url) {
file_name<-url|>fs::path_file() |>stringr::str_remove("\\?.*$")
}
# Main function to download the latest FFS file and return it as a dataframeget_latest_ffs_data<-function() {
# This url will automatically navigate to the latest file version (e.g. medi_cal_ffs_provider_listing_9_25_24.csv)url<-"https://data.chhs.ca.gov/dataset/profile-of-enrolled-medi-cal-fee-for-service-ffs-providers/resource/d652b210-ec3d-4a92-b7e0-e55c3dcbc7dc/download"response<-httr2::request(url) |>httr2::req_perform()
# Guard clause to handle invalid HTTP status codesstatus_code<-httr2::resp_status(response)
if (status_code!=200L) {
stop(glue::glue("Invalid HTTP status code {status_code} returned when querying {url}"))
}
# Guard clause to handle invalid content type (if the URL returns anything besides a text file)content_type<-httr2::resp_header(response, "Content-Type")
if (stringr::str_detect(content_type, "text/csv", negate=TRUE)) {
stop(glue::glue("Invalid content type {content_type} returned when querying {url}"))
}
# Extract the monthly file name from the URLfile_name<- get_file_name_from_url(response$url)
file_path<-fs::path(tempdir(), file_name)
# Save the response body to a local temp file
writeBin(response$body, file_path)
# Read in the csv contents as a dataframedf<-readr::read_csv(file_path) |>janitor::clean_names() |># Include the basename of the source file as a new columndplyr::mutate(
source_file= {{ file_name }}
)
return(df)
}
df<- get_latest_ffs_data()
The text was updated successfully, but these errors were encountered:
I was working on a separate project that relied on the FFS data. It might be interesting to include this data source within this project and compare it against the managed care provider data.
The text was updated successfully, but these errors were encountered: