-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathredcapExport.R
192 lines (169 loc) · 7.97 KB
/
redcapExport.R
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
redcapExportMeta <- function(APIKEY, URI='https://redcap.vanderbilt.edu/api/') {
if (!require('RCurl')) {
stop('RCurl is not installed')
}
meta_data <- read.csv(text=postForm(uri=URI, token=APIKEY, content='metadata',
format='csv',
# RCurl options
.opts=curlOptions(ssl.verifyhost=2)),
stringsAsFactors=FALSE, na.strings='')
subset(meta_data, field_type != 'field_type')
}
redcapExportArms <- function(APIKEY, URI='https://redcap.vanderbilt.edu/api/') {
if (!require('RCurl')) {
stop('RCurl is not installed')
}
read.csv(text=postForm(uri=URI, token=APIKEY, content='arm',
format='csv',
# RCurl options
.opts=curlOptions(ssl.verifyhost=2)),
stringsAsFactors=FALSE, na.strings='')
}
redcapExportEvents <- function(APIKEY, URI='https://redcap.vanderbilt.edu/api/') {
if (!require('RCurl')) {
stop('RCurl is not installed')
}
read.csv(text=postForm(uri=URI, token=APIKEY, content='event',
format='csv',
# RCurl options
.opts=curlOptions(ssl.verifyhost=2)),
stringsAsFactors=FALSE, na.strings='')
}
redcapExportMappings <- function(APIKEY, URI='https://redcap.vanderbilt.edu/api/') {
if (!require('RCurl')) {
stop('RCurl is not installed')
}
read.csv(text=postForm(uri=URI, token=APIKEY, content='formEventMapping',
format='csv',
# RCurl options
.opts=curlOptions(ssl.verifyhost=2)),
stringsAsFactors=FALSE, na.strings='')
}
redcapExportRecords <- function(APIKEY, URI='https://redcap.vanderbilt.edu/api/', format='csv', type='flat', rawOrLabel='raw', exportCheckboxLabel='false', forms=NULL, fields=NULL, events=NULL) {
if (!require('RCurl')) {
stop('RCurl is not installed')
}
read.csv(text=postForm(uri=URI, token=APIKEY, content='record',
format=format, type=type,
# Redcap API options
rawOrLabel=rawOrLabel,
exportCheckboxLabel=exportCheckboxLabel,
forms=forms, fields=fields, events=events,
# RCurl options
.opts=curlOptions(ssl.verifyhost=2)),
stringsAsFactors=FALSE, na.strings='')
}
redcapExport <- function(APIKEY, URI='https://redcap.vanderbilt.edu/api/', labels=TRUE, checkboxLabels=FALSE, forms=NULL, fields=NULL, events=NULL) {
if (!require('RCurl')) {
stop('RCurl is not installed')
}
rmq <- function(x) gsub("['\"]", '', x)
clean <- function(x) { gsub('[\n]', ' ', x) }
# if Hmisc is available, apply labels
Hmisc <- require('Hmisc')
# Fetch metadata
meta_data <- subset(redcapExportMeta(APIKEY, URI), field_type %in% c('text','notes','dropdown','radio','checkbox','calc','slider','yesno','truefalse'))
form_field_names <- sprintf('%s_complete', unique(meta_data$form_name))
if (!is.null(forms)) {
forms <- intersect(forms, unique(meta_data$form_name))
form_field_names <- sprintf('%s_complete', forms)
}
if (!is.null(fields)) {
form_fields <- subset(meta_data, form_name %in% forms)$field_name # Select all the field (variable) names that are in the forms the user specified.
if (!all(fields %in% form_fields)) {
specific_fields <- intersect(fields, c(unique(meta_data$field_name), form_field_names))
fields <- union(form_fields, specific_fields)
if (!is.null(forms)) {
fields <- c(fields, form_field_names)
forms <- NULL
}
form_field_names <- intersect(form_field_names, fields)
}
}
if (length(forms) > 0) {
meta_data <- subset(meta_data, meta_data$form_name %in% forms)
} else if (length(fields) > 0) {
meta_data <- subset(meta_data, meta_data$field_name %in% fields)
}
# Fetch records
data <- redcapExportRecords(APIKEY, URI=URI,
# Redcap API options
rawOrLabel=c('raw','label')[1 + labels], # real values or codes
exportCheckboxLabel=c('false','true')[1 + checkboxLabels], # real values or checked/unchecked
forms=paste(forms, collapse=','),
fields=paste(fields, collapse=','),
events=paste(events, collapse=','))
for (i in seq_len(nrow(meta_data))) {
fld <- as.list(meta_data[i,])
choices <- redcapExtractChoices(fld$select_choices_or_calculations)
nums <- choices$numbers
choices <- choices$labels
if (fld$field_type == 'checkbox') {
for (j in seq(length(nums))) {
checkbox_name <- sprintf('%s___%s', fld$field_name, nums[j])
# advance to next field if not in dataset
if (is.null(data[[checkbox_name]])) next
if (labels) {
if (checkboxLabels) {
levels <- choices[j]
} else {
levels <- c('Unchecked', 'Checked')
}
data[[checkbox_name]] <- factor(data[[checkbox_name]], levels=levels)
} else {
data[[checkbox_name]] <- factor(data[[checkbox_name]], levels=c('0','1'))
}
if (Hmisc) {
label(data[[checkbox_name]]) <- sprintf('%s (choice=%s)', clean(fld$field_label), rmq(choices[j]))
}
}
} else {
# advance to next field if not in dataset
if (is.null(data[[fld$field_name]])) next
if (fld$field_type %in% c('radio','dropdown','yesno','truefalse')) {
if (labels) {
if (fld$field_type == 'yesno') {
levels <- c('No','Yes')
} else if (fld$field_type == 'truefalse') {
levels <- c('False','True')
} else {
levels <- choices
}
data[[fld$field_name]] <- factor(data[[fld$field_name]], levels=levels)
} else {
if (fld$field_type == 'yesno') {
levels <- c('0','1')
} else if (fld$field_type == 'truefalse') {
levels <- c('0','1')
} else {
levels <- choices
}
data[[fld$field_name]] <- factor(data[[fld$field_name]], levels=levels)
}
} else if ((!is.na(fld$text_validation_type_or_show_slider_number) &&
fld$text_validation_type_or_show_slider_number %in% c('float','int') ) ||
fld$field_type %in% c('calc') ) {
suppressWarnings(data[[fld$field_name]] <- as.numeric(data[[fld$field_name]]))
}
if (Hmisc) {
label(data[[fld$field_name]]) <- fld$field_label
}
}
}
for (form_field_name in form_field_names) {
data[[form_field_name]] <- factor(data[[form_field_name]], levels=c('Incomplete','Unverified','Complete'))
if (Hmisc) {
label(data[[form_field_name]]) <- 'Complete?'
}
}
data
}
redcapExtractChoices <- function(choices) {
# extract checkbox choices to identify sub-variables in data
choices <- strsplit(choices, ' *[|] *')[[1]]
choices <- sub('^[ ]+(.*)$', '\\1', choices)
choices <- sub('(.*)[ ]+$', '\\1', choices)
nums <- sub('^([^,]+).*$', '\\1', choices)
choices <- sub('^[^,]+[, ]+(.*)$', '\\1', choices)
list(numbers=nums, labels=choices)
}