Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FFS data? #1

Open
philiporlando opened this issue Sep 30, 2024 · 0 comments
Open

Add FFS data? #1

philiporlando opened this issue Sep 30, 2024 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@philiporlando
Copy link
Owner

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 url
get_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 dataframe
get_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 codes
  status_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 URL
  file_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 dataframe
  df <- readr::read_csv(file_path) |> 
    janitor::clean_names() |> 
    # Include the basename of the source file as a new column
    dplyr::mutate(
      source_file = {{ file_name }}
    )
  
  return(df)
}

df <- get_latest_ffs_data()
@philiporlando philiporlando self-assigned this Sep 30, 2024
@philiporlando philiporlando added the enhancement New feature or request label Sep 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant