Last active
May 29, 2025 21:19
-
-
Save benjamin-chan/e5a49c2cfd2e2752a20282e7110d3d59 to your computer and use it in GitHub Desktop.
R function to query CMS data via API
This file contains hidden or 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(datasetID, | |
| columns = "*", | |
| where = NULL, | |
| limit = 0, | |
| root = "https://data.cms.gov/provider-data/api/1/datastore/sql") | |
| { | |
| require(magrittr) | |
| require(httr) | |
| require(jsonlite) | |
| url <- sprintf("https://data.cms.gov/provider-data/api/1/metastore/schemas/dataset/items/%s?show-reference-ids", datasetID) | |
| temp <- GET(url)$content %>% rawToChar() %>% fromJSON() | |
| distributionId <- temp[["distribution"]][["identifier"]] | |
| 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("xubh-q36u", where = 'state = \"OR\"') # Hospital General Information | |
| # getCMS("ynj2-r877", where = 'facility_id = \"380009\"') # Complications and Deaths - Hospital |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
23ew-n7w92fpu-cgbb2rkq-ygai59mq-zhtsutgq-v46wm5eg-upu56jpm-sxkc97z8-de9695rg-2uspyc9t-dgbk252m-zfp9gxki-hrr87cv8-v37d48nr-hqxx4jcv-atw7wue8-3vwetf3h-mrrsaxe7-s95ex663-bwbjtqkv-mgxqmuwa-ieneynj2-r877qqw3-t4iebs2r-24vhy9us-9xdfe6hj-umamxrgf-x36b5gv4-jwyvu625-zae7vtqa-m4zndgck-syfz99ue-w85f84jm-wiui77hc-ibv8yd3s-jyhdk2ze-bqvwyizn-abxns5pj-hua36pfg-whmxxubh-q36uz8ax-x9j1jfnd-nl7syq43-i98g5hk7-b79mk653-4ka8iy27-wz379g7e-btytqatj-nmws9n3s-kdb3pudb-wetrsu9h-3pvjavtz-f2gedgmq-aat3ypbt-wvdkq9vs-r7wps5xg-sys6dc76-gh7x4j6d-yzcenrth-mfg3rrqw-56er3n5g-6b7frs6n-9qwgwkfw-kthedi9i-zzrcif5v-4x48ygty-mm5ac7us-v4mfyv7e-xc69isrn-hqyyapyc-v239632h-zacacvcs-xecj4gkm-5ypv6qxe-iqz8ptds-r8imgbq5-7hzruyx4-5s7fbzsr-4my47t8x-u3irv9e4-nwhhka5z-ibe3azum-44ivfp6g-2gsnifjz-ge4wr5ix-sfxwdjen-97juijh5-nb2vy2hd-n93eg6vv-u9sr4pq5-n9pyfykj-qjee6uyb-waubxcdc-v8bmtbry-pc2d284v-j9fzujcx-uautqmdc-9999tagd-9999mj5m-pzi60ba7-2cb08c70-d3537d6a-e7a6a174-a962svdt-c123hicp-9999ccn4-8vbyvxub-6swim5jg-jg7ifa88-6ff23614-1eefbdd5-4a044269-8a742fbd-6172075a-d4873f64-129eea2d-94679873-722a3a83-ee2d98d4-087194f7-ab53adba-1d45bd7d-078ac8a8-342e32d8-3235d796-7f06c14e-64926b8f-5372b554-ef7ebb4c-dcdf501b-ecd42e55-8767fbfb-4b94bce0-b5db0f37-cab0e84d-d357c44d-bde6e1a1-c9b42483-f62bd150-d1410ddf-4325a0cb-cdabe2bb-d3719735-71766787-a1bfb06f-0828b9bf-688386e1-d1d15d65-6dcf9b4a-75d50938-3dfa8c8a-b9110b1a-da3ff90c-2246d0ce-5cad57e0-2991069d-826b5f44-cb84d640-e5288c6b-fe40c382-eab78753-38d14a4e-e0dacfa7-e909914a-7700d226-80a4ccbb-cbfa5e3a-bee4029b-dd7e0d7d-e98832b2-9f888634-f6a70330-b6e0057a-5bcf92bb-de792ca5-10070127-af37288b-4bed8283-8a65385c-8c9739da-b8abc713-00e80f6a-98c35d9c-1e86d1c9-d5b4993f-4504afcb-07d8f226-42b746dc-a66cpvax-cv19avax-cv19a55e-5b88eda0-92f0ecb7-cb46b599-54c1tee5-ixt5c886-nwpj98ix-2iqynrdb-3fcyct36-nrcqnasn-k89k5zdx-ny2x5sqm-2qkuhanv-ru8h3xeb-u9wp27ea-46a8n0yb-utilclinical_depressioncomplete_qip_dataktv_comprehensivefootnoteshypercalcemianhsn_denhsn_bsisrrstrrtpsvat_topicufrshrmedrecpppwqip_ich_cahpsmxtu-43qsqoeg-w7ckqigt-w5cxe491-a466730a-fcd0cd1d-5f844533-9861254e-694cv6ff-cmgx5ryi-3zdnanfs-nlq7covid-19_hcp56d7-49944c74-462ef4ga-b9gx