Last active
September 25, 2024 21:06
-
-
Save benjamin-chan/e5a49c2cfd2e2752a20282e7110d3d59 to your computer and use it in GitHub Desktop.
R function to query CMS data via API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
getCMS <- function(distributionId, | |
columns = "*", | |
where = NULL, | |
limit = 0, | |
root = "https://data.cms.gov/provider-data/api/1/datastore/sql") | |
{ | |
require(magrittr) | |
require(httr) | |
require(jsonlite) | |
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() | |
} | |
# EXAMPLE | |
# getCMS("10ab1332-3857-537a-8ffa-cbe91dbe4c65", where = 'state = \"OR\"') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment