Skip to content

Instantly share code, notes, and snippets.

@benjamin-chan
Created May 24, 2024 22:44
Show Gist options
  • Save benjamin-chan/e5a49c2cfd2e2752a20282e7110d3d59 to your computer and use it in GitHub Desktop.
Save benjamin-chan/e5a49c2cfd2e2752a20282e7110d3d59 to your computer and use it in GitHub Desktop.
R function to query CMS data via API
getCMS <- function(distributionId,
columns = "*",
where = NULL,
limit = 0)
{
require(magrittr)
require(httr)
require(jsonlite)
root <- "https://data.cms.gov/provider-data/api/1/datastore/sql"
if (is.null(where)) {
query <- sprintf("[SELECT %s FROM %s][LIMIT %d]", columns, distributionId, limit)
} else {
query <- sprintf("[SELECT %s FROM %s][WHERE %s][LIMIT %d]", columns, distributionId, where, limit)
}
options <- "show_db_columns=true"
url <- sprintf("%s?query=%s&%s", root, query, options) %>% URLencode()
GET(url)$content %>% rawToChar() %>% fromJSON()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment