This vignette covers the the basic functions exposed by the httr
and curl
packages which allow you to interact with REDCap through its API.
The function redcap_read_oneshot
uses the httr
package to call the REDCap API.
## Loading required namespace: kableExtra
There is some information that is specific to the REDCap project, as opposed to an individual operation. This includes the (1) uri of the server, and the (2) token for the user’s project.
library(REDCapR) #Load the package into the current R session.
uri <- "https://bbmc.ouhsc.edu/redcap/api/"
token <- "9A81268476645C4E5F03428B8AC3AA7B" #`UnitTestPhiFree` user and simple project (pid 153)
If no information is passed about the desired records or fields, then the entire data set is returned. Only two parameters are required, redcap_uri
and token
. Unless the verbose
parameter is set to FALSE
, a message will be printed on the R console with the number of records and fields returned.
#Return all records and all variables.
ds_all_rows_all_fields <- redcap_read(redcap_uri=uri, token=token)$data
The data dictionary describing 16 fields was read from REDCap in 0.8 seconds. The http status code was 200.
5 records and 1 columns were read from REDCap in 0.5 seconds. The http status code was 200.
Starting to read 5 records at 2017-05-18 12:45:08
Reading batch 1 of 1, with subjects 1 through 5 (ie, 5 unique subject records).
5 records and 24 columns were read from REDCap in 0.4 seconds. The http status code was 200.
ds_all_rows_all_fields #Inspect the returned dataset
record id | name first | name last | address | telephone | dob | age | sex | demographics complete | height | weight | bmi | comments | mugshot | health complete | race 1 | race 2 | race 3 | race 4 | race 5 | race 6 | ethnicity | race and ethnicity complete | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | Nutmeg | Nutmouse | 14 Rose Cottage St. Kenning UK, 323232 |
|
nutty@mouse.com | 2003-08-30 | 11 | 0 | 2 | 7.00 | 1 | 204.1 | Character in a book, with some guessing | [document] | 1 | 0 | 0 | 0 | 0 | 1 | 0 | 1 | 2 |
2 | Tumtum | Nutmouse | 14 Rose Cottage Blvd. Kenning UK 34243 |
|
tummy@mouse.comm | 2003-03-10 | 11 | 1 | 2 | 6.00 | 1 | 277.8 | A mouse character from a good book | [document] | 0 | 0 | 0 | 1 | 0 | 1 | 0 | 1 | 0 |
3 | Marcus | Wood | 243 Hill St. Guthrie OK 73402 |
|
mw@mwood.net | 1934-04-09 | 80 | 1 | 2 | 180.00 | 80 | 24.7 | completely made up | [document] | 2 | 0 | 0 | 0 | 1 | 1 | 0 | 0 | 2 |
4 | Trudy | DAG | 342 Elm Duncanville TX, 75116 |
|
peroxide@blonde.com | 1952-11-02 | 61 | 0 | 2 | 165.00 | 54 | 19.8 | This record doesn’t have a DAG assigned So call up Trudy on the telephone Send her a letter in the mail | [document] | 2 | 0 | 1 | 0 | 0 | 1 | 0 | 1 | 2 |
5 | John Lee | Walker | Hotel Suite New Orleans LA, 70115 |
|
left@hippocket.com | 1955-04-15 | 59 | 1 | 2 | 193.04 | 104 | 27.9 | Had a hand for trouble and a eye for cash He had a gold watch chain and a black mustache | [document] | 0 | 1 | 0 | 0 | 0 | 0 | 1 | 2 | 2 |
If only a subset of the records is desired, the two approaches are shown below. The first is to pass an array (where each element is an ID) to the records
parameter. The second is to pass a single string (where the elements are separated by commas) to the records_collapsed
parameter.
The first format is more natural for more R users. The second format is what is expected by the REDCap API. If a value for records
is specified, but records_collapsed
is not specified, then redcap_read_oneshot
automatically converts the array into the format needed by the API.
#Return only records with IDs of 1 and 3
desired_records_v1 <- c(1, 3)
ds_some_rows_v1 <- redcap_read(
redcap_uri = uri,
token = token,
records = desired_records_v1
)$data
The data dictionary describing 16 fields was read from REDCap in 0.4 seconds. The http status code was 200.
2 records and 1 columns were read from REDCap in 0.4 seconds. The http status code was 200.
Starting to read 2 records at 2017-05-18 12:45:10
Reading batch 1 of 1, with subjects 1 through 3 (ie, 2 unique subject records).
2 records and 24 columns were read from REDCap in 0.4 seconds. The http status code was 200.
#Return only records with IDs of 1 and 3 (alternate way)
desired_records_v2 <- "1, 3"
ds_some_rows_v2 <- redcap_read(
redcap_uri = uri,
token = token,
records_collapsed = desired_records_v2
)$data
The data dictionary describing 16 fields was read from REDCap in 0.4 seconds. The http status code was 200.
2 records and 1 columns were read from REDCap in 0.4 seconds. The http status code was 200.
Starting to read 2 records at 2017-05-18 12:45:14
Reading batch 1 of 1, with subjects 1 through 3 (ie, 2 unique subject records).
2 records and 24 columns were read from REDCap in 0.3 seconds. The http status code was 200.
ds_some_rows_v2 #Inspect the returned dataset
record id | name first | name last | address | telephone | dob | age | sex | demographics complete | height | weight | bmi | comments | mugshot | health complete | race 1 | race 2 | race 3 | race 4 | race 5 | race 6 | ethnicity | race and ethnicity complete | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | Nutmeg | Nutmouse | 14 Rose Cottage St. Kenning UK, 323232 |
|
nutty@mouse.com | 2003-08-30 | 11 | 0 | 2 | 7 | 1 | 204.1 | Character in a book, with some guessing | [document] | 1 | 0 | 0 | 0 | 0 | 1 | 0 | 1 | 2 |
3 | Marcus | Wood | 243 Hill St. Guthrie OK 73402 |
|
mw@mwood.net | 1934-04-09 | 80 | 1 | 2 | 180 | 80 | 24.7 | completely made up | [document] | 2 | 0 | 0 | 0 | 1 | 1 | 0 | 0 | 2 |
If only a subset of the fields is desired, then two approaches exist. The first is to pass an array (where each element is an field) to the fields
parameter. The second is to pass a single string (where the elements are separated by commas) to the fields_collapsed
parameter. Like with records
and records_collapsed
described above, this function converts the more natural format (ie, fields
) to the format required by the API (ie, fields_collapsed
) if fields
is specified and fields_collapsed
is not.
#Return only the fields record_id, name_first, and age
desired_fields_v1 <- c("record_id", "name_first", "age")
ds_some_fields_v1 <- redcap_read(
redcap_uri = uri,
token = token,
fields = desired_fields_v1
)$data
The data dictionary describing 16 fields was read from REDCap in 0.2 seconds. The http status code was 200.
5 records and 1 columns were read from REDCap in 0.4 seconds. The http status code was 200.
Starting to read 5 records at 2017-05-18 12:45:16
Reading batch 1 of 1, with subjects 1 through 5 (ie, 5 unique subject records).
5 records and 3 columns were read from REDCap in 0.4 seconds. The http status code was 200.
#Return only the fields record_id, name_first, and age (alternate way)
desired_fields_v2 <- "record_id, name_first, age"
ds_some_fields_v2 <- redcap_read(
redcap_uri = uri,
token = token,
fields_collapsed = desired_fields_v2
)$data
The data dictionary describing 16 fields was read from REDCap in 0.4 seconds. The http status code was 200.
5 records and 1 columns were read from REDCap in 0.2 seconds. The http status code was 200.
Starting to read 5 records at 2017-05-18 12:45:18
Reading batch 1 of 1, with subjects 1 through 5 (ie, 5 unique subject records).
5 records and 3 columns were read from REDCap in 0.4 seconds. The http status code was 200.
ds_some_fields_v2 #Inspect the returned dataset
record id | name first | age |
---|---|---|
1 | Nutmeg | 11 |
2 | Tumtum | 11 |
3 | Marcus | 80 |
4 | Trudy | 61 |
5 | John Lee | 59 |
The two techniques above can be combined when your datasets are large and you don’t want to pull records with certain values. Suppose you want to select subjects from the previous dataset if the were born before 1960 and their weight was over 70kg. Two calls to the server are required. The first call to REDCap pulls all the records, but for only three columns: record_id
, dob
, and weight
. From this subset, identify the records that you want to pull all the data for; in this case, the desired record_id
values are 3
& 5
. The second call to REDCap pulls all the columns, but only for the identified records.
######
## Step 1: First call to REDCap
desired_fields_v3 <- c("record_id", "dob", "weight")
ds_some_fields_v3 <- redcap_read(
redcap_uri = uri,
token = token,
fields = desired_fields_v3
)$data
The data dictionary describing 16 fields was read from REDCap in 0.4 seconds. The http status code was 200.
5 records and 1 columns were read from REDCap in 0.4 seconds. The http status code was 200.
Starting to read 5 records at 2017-05-18 12:45:19
Reading batch 1 of 1, with subjects 1 through 5 (ie, 5 unique subject records).
5 records and 3 columns were read from REDCap in 0.4 seconds. The http status code was 200.
ds_some_fields_v3 #Examine the these three variables.
record id | dob | weight |
---|---|---|
1 | 2003-08-30 | 1 |
2 | 2003-03-10 | 1 |
3 | 1934-04-09 | 80 |
4 | 1952-11-02 | 54 |
5 | 1955-04-15 | 104 |
######
## Step 2: identify desired records, based on age & weight
before_1960 <- (ds_some_fields_v3$dob <= as.Date("1960-01-01"))
heavier_than_70_kg <- (ds_some_fields_v3$weight > 70)
desired_records_v3 <- ds_some_fields_v3[before_1960 & heavier_than_70_kg, ]$record_id
desired_records_v3 #Peek at IDs of the identified records
[1] 3 5
######
## Step 3: second call to REDCap
#Return only records that met the age & weight criteria.
ds_some_rows_v3 <- redcap_read(
redcap_uri = uri,
token = token,
records = desired_records_v3
)$data
The data dictionary describing 16 fields was read from REDCap in 0.3 seconds. The http status code was 200.
2 records and 1 columns were read from REDCap in 0.4 seconds. The http status code was 200.
Starting to read 2 records at 2017-05-18 12:45:21
Reading batch 1 of 1, with subjects 3 through 5 (ie, 2 unique subject records).
2 records and 24 columns were read from REDCap in 0.4 seconds. The http status code was 200.
ds_some_rows_v3 #Examine the results.
record id | name first | name last | address | telephone | dob | age | sex | demographics complete | height | weight | bmi | comments | mugshot | health complete | race 1 | race 2 | race 3 | race 4 | race 5 | race 6 | ethnicity | race and ethnicity complete | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
3 | Marcus | Wood | 243 Hill St. Guthrie OK 73402 |
|
mw@mwood.net | 1934-04-09 | 80 | 1 | 2 | 180.00 | 80 | 24.7 | completely made up | [document] | 2 | 0 | 0 | 0 | 1 | 1 | 0 | 0 | 2 |
5 | John Lee | Walker | Hotel Suite New Orleans LA, 70115 |
|
left@hippocket.com | 1955-04-15 | 59 | 1 | 2 | 193.04 | 104 | 27.9 | Had a hand for trouble and a eye for cash He had a gold watch chain and a black mustache | [document] | 0 | 1 | 0 | 0 | 0 | 0 | 1 | 2 | 2 |
The examples above have shown only the resulting data.frame
, by specifying $data
at the end of the call. However, more is available to those wanting additional information, such as:
data
object has the data.frame
, as in the previous examples.success
boolean value indicates if redcap_read_oneshot
believes the operation completed as intended.status_codes
is a collection of http status codes, separated by semicolons. There is one code for each batch attempted.outcome_messages
: A collection of human readable strings indicating the operations’ semicolons. There is one code for each batch attempted. In an unsuccessful operation, it should contain diagnostic information.records_collapsed
field passed to the API. This shows which record subsets, if any, were requested.fields_collapsed
fields passed to the API. This shows which field subsets, if any, were requested.elapsed_seconds
measures the duration of the call.#Return only the fields record_id, name_first, and age
all_information <- redcap_read(
redcap_uri = uri,
token = token,
fields = desired_fields_v1
)
The data dictionary describing 16 fields was read from REDCap in 0.3 seconds. The http status code was 200.
5 records and 1 columns were read from REDCap in 0.5 seconds. The http status code was 200.
Starting to read 5 records at 2017-05-18 12:45:23
Reading batch 1 of 1, with subjects 1 through 5 (ie, 5 unique subject records).
5 records and 3 columns were read from REDCap in 0.4 seconds. The http status code was 200.
all_information #Inspect the additional information
$data
record_id name_first age
1 1 Nutmeg 11
2 2 Tumtum 11
3 3 Marcus 80
4 4 Trudy 61
5 5 John Lee 59
$success
[1] TRUE
$status_codes
[1] "200"
$outcome_messages
[1] "5 records and 3 columns were read from REDCap in 0.4 seconds. The http status code was 200."
$records_collapsed
[1] ""
$fields_collapsed
[1] "record_id,name_first,age"
$filter_logic
[1] ""
$events_collapsed
[1] ""
$elapsed_seconds
[1] 1.771707
For the sake of documentation and reproducibility, the current report was rendered in the following environment. Click the line below to expand.
Environment
Session info -------------------------------------------------------------
setting value
version R version 3.4.0 (2017-04-21)
system x86_64, linux-gnu
ui X11
language en_US
collate C
tz America/Chicago
date 2017-05-18
Packages -----------------------------------------------------------------
package * version date source
backports 1.0.5 2017-01-18 CRAN (R 3.3.1)
base * 3.4.0 2017-04-21 local
compiler 3.4.0 2017-04-21 local
curl 2.6 2017-04-27 CRAN (R 3.4.0)
data.table 1.10.4 2017-02-01 CRAN (R 3.3.1)
datasets * 3.4.0 2017-04-21 local
devtools 1.13.1 2017-05-13 CRAN (R 3.4.0)
digest 0.6.12 2017-01-27 CRAN (R 3.3.1)
evaluate 0.10 2016-10-11 CRAN (R 3.3.1)
graphics * 3.4.0 2017-04-21 local
grDevices * 3.4.0 2017-04-21 local
highr 0.6 2016-05-09 CRAN (R 3.3.0)
htmltools 0.3.6 2017-04-28 CRAN (R 3.4.0)
httr 1.2.1 2016-07-03 CRAN (R 3.3.1)
kableExtra 0.1.0 2017-03-02 CRAN (R 3.3.1)
knitr * 1.16 2017-05-18 CRAN (R 3.4.0)
magrittr * 1.5 2014-11-22 CRAN (R 3.3.0)
memoise 1.1.0 2017-04-21 CRAN (R 3.4.0)
methods * 3.4.0 2017-04-21 local
R6 2.2.1 2017-05-10 CRAN (R 3.4.0)
Rcpp 0.12.10 2017-03-19 CRAN (R 3.3.1)
REDCapR * 0.9.8 2017-05-18 local
rlang 0.1.1 2017-05-18 CRAN (R 3.4.0)
rmarkdown 1.5 2017-04-26 CRAN (R 3.4.0)
rprojroot 1.2 2017-01-16 CRAN (R 3.3.1)
rvest 0.3.2 2016-06-17 CRAN (R 3.3.1)
selectr 0.3-1 2016-12-19 CRAN (R 3.3.1)
stats * 3.4.0 2017-04-21 local
stringi 1.1.5 2017-04-07 CRAN (R 3.3.3)
stringr 1.2.0 2017-02-18 CRAN (R 3.3.1)
tibble 1.3.1 2017-05-18 Github (tidyverse/tibble@8f30072)
tools 3.4.0 2017-04-21 local
utils * 3.4.0 2017-04-21 local
withr 1.0.2 2016-06-20 CRAN (R 3.3.0)
XML 3.98-1.7 2017-05-03 CRAN (R 3.4.0)
xml2 1.1.1 2017-01-24 CRAN (R 3.3.1)
yaml 2.1.14 2016-11-12 CRAN (R 3.3.1)
Report rendered by wibeasley at 2017-05-18, 12:45 -0500 in 19 seconds.