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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
23ew-n7w9
2fpu-cgbb
2rkq-ygai
59mq-zhts
utgq-v46w
m5eg-upu5
6jpm-sxkc
97z8-de96
95rg-2usp
yc9t-dgbk
252m-zfp9
gxki-hrr8
7cv8-v37d
48nr-hqxx
4jcv-atw7
wue8-3vwe
tf3h-mrrs
axe7-s95e
x663-bwbj
tqkv-mgxq
muwa-iene
ynj2-r877
qqw3-t4ie
bs2r-24vh
y9us-9xdf
e6hj-umam
xrgf-x36b
5gv4-jwyv
u625-zae7
vtqa-m4zn
dgck-syfz
99ue-w85f
84jm-wiui
77hc-ibv8
yd3s-jyhd
k2ze-bqvw
yizn-abxn
s5pj-hua3
6pfg-whmx
xubh-q36u
z8ax-x9j1
jfnd-nl7s
yq43-i98g
5hk7-b79m
k653-4ka8
iy27-wz37
9g7e-btyt
qatj-nmws
9n3s-kdb3
pudb-wetr
su9h-3pvj
avtz-f2ge
dgmq-aat3
ypbt-wvdk
q9vs-r7wp
s5xg-sys6
dc76-gh7x
4j6d-yzce
nrth-mfg3
rrqw-56er
3n5g-6b7f
rs6n-9qwg
wkfw-kthe
di9i-zzrc
if5v-4x48
ygty-mm5a
c7us-v4mf
yv7e-xc69
isrn-hqyy
apyc-v239
632h-zaca
cvcs-xecj
4gkm-5ypv
6qxe-iqz8
ptds-r8im
gbq5-7hzr
uyx4-5s7f
bzsr-4my4
7t8x-u3ir
v9e4-nwhh
ka5z-ibe3
azum-44iv
fp6g-2gsn
ifjz-ge4w
r5ix-sfxw
djen-97ju
ijh5-nb2v
y2hd-n93e
g6vv-u9sr
4pq5-n9py
fykj-qjee
6uyb-waub
xcdc-v8bm
tbry-pc2d
284v-j9fz
ujcx-uaut
qmdc-9999
tagd-9999
mj5m-pzi6
0ba7-2cb0
8c70-d353
7d6a-e7a6
a174-a962
svdt-c123
hicp-9999
ccn4-8vby
vxub-6swi
m5jg-jg7i
fa88-6ff2
3614-1eef
bdd5-4a04
4269-8a74
2fbd-6172
075a-d487
3f64-129e
ea2d-9467
9873-722a
3a83-ee2d
98d4-0871
94f7-ab53
adba-1d45
bd7d-078a
c8a8-342e
32d8-3235
d796-7f06
c14e-6492
6b8f-5372
b554-ef7e
bb4c-dcdf
501b-ecd4
2e55-8767
fbfb-4b94
bce0-b5db
0f37-cab0
e84d-d357
c44d-bde6
e1a1-c9b4
2483-f62b
d150-d141
0ddf-4325
a0cb-cdab
e2bb-d371
9735-7176
6787-a1bf
b06f-0828
b9bf-6883
86e1-d1d1
5d65-6dcf
9b4a-75d5
0938-3dfa
8c8a-b911
0b1a-da3f
f90c-2246
d0ce-5cad
57e0-2991
069d-826b
5f44-cb84
d640-e528
8c6b-fe40
c382-eab7
8753-38d1
4a4e-e0da
cfa7-e909
914a-7700
d226-80a4
ccbb-cbfa
5e3a-bee4
029b-dd7e
0d7d-e988
32b2-9f88
8634-f6a7
0330-b6e0
057a-5bcf
92bb-de79
2ca5-1007
0127-af37
288b-4bed
8283-8a65
385c-8c97
39da-b8ab
c713-00e8
0f6a-98c3
5d9c-1e86
d1c9-d5b4
993f-4504
afcb-07d8
f226-42b7
46dc-a66c
pvax-cv19
avax-cv19
a55e-5b88
eda0-92f0
ecb7-cb46
b599-54c1
tee5-ixt5
c886-nwpj
98ix-2iqy
nrdb-3fcy
ct36-nrcq
nasn-k89k
5zdx-ny2x
5sqm-2qku
hanv-ru8h
3xeb-u9wp
27ea-46a8
n0yb-util
clinical_depression
complete_qip_data
ktv_comprehensive
footnotes
hypercalcemia
nhsn_de
nhsn_bsi
srr
strr
tps
vat_topic
ufr
shr
medrec
pppw
qip_ich_cahps
mxtu-43qs
qoeg-w7ck
qigt-w5cx
e491-a466
730a-fcd0
cd1d-5f84
4533-9861
254e-694c
v6ff-cmgx
5ryi-3zdn
anfs-nlq7
covid-19_hcp
56d7-4994
4c74-462e
f4ga-b9gx