forked from SurgicalInformatics/cocin_ccp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01_data_pull.R
More file actions
50 lines (43 loc) · 1.26 KB
/
01_data_pull.R
File metadata and controls
50 lines (43 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# ISARIC REDCap database analysis: API pull
# API pull from Oxford REDCap server
# Centre for Medical Informatics, Usher Institute, University of Edinburgh 2020
# To use this, set your REDCap API token as an environment variable.
## Uncomment and run the following line:
# usethis::edit_r_environ()
## this opens up .Renviron, add your token, e.g. ccp_token = 2F3xxxxxxxxxxxxE0111
## Restart R
# 1. API pull
# 2. Apply REDCap R formatitng, file edited.
# 3. Final object created: ccp_data
# Libraries
library(RCurl)
library(tidyverse)
# API pull
## The API call fail randomly due to traffic
## Try 5 times then stop
tries = 0
data = NA
while (tries == 0 | (tries < 5 & inherits(data, "try-error"))){
data = try(postForm(
uri='https://ncov.medsci.ox.ac.uk/api/',
token=Sys.getenv("ccp_token"),
content='record',
format='csv',
type='flat',
rawOrLabel='raw',
rawOrLabelHeaders='raw',
exportCheckboxLabel='false',
exportSurveyFields='false',
exportDataAccessGroups='true',
returnFormat='json'
))
tries = tries + 1
# let's wait a second letting the API cool off
Sys.sleep(1)
}
data = read_csv(data, na = "", guess_max = 20000)
# Formating
source("CCPUKSARI_R_2020-03-04_1532.R")
# Out object and clean
ccp_data = data
rm(data, tries)