-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path01_data_pull.R
More file actions
53 lines (45 loc) · 1.39 KB
/
01_data_pull.R
File metadata and controls
53 lines (45 loc) · 1.39 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
51
52
53
# 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_pull = NA
while (tries == 0 | (tries < 5 & inherits(data_pull, "try-error"))){
data_pull = 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_pull, na = "", col_types = cols(.default = col_character())) %>%
type_convert()
#write_rds(data_pull, path = "data_pull_2020-06-29_1632.rds")
# Formating
source("CCPUKSARI_R_2020-06-26_1323.r")
# Out object and clean
ccp_data = data
rm(data, tries)