Basic REDCapR Operations

2019-09-22

This vignette covers the the basic functions exposed by the httr and curl packages which allow you to interact with REDCap through its API.

Reading REDCap Data

The function redcap_read_oneshot uses the httr package to call the REDCap API.

## Loading required namespace: kableExtra

Set project-wide values.

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.

Read all records and fields.

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.

The data dictionary describing 16 fields was read from REDCap in 1 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 2019-09-22 17:46:21.
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 1.3 seconds.  The http status code was 200.
  record_id name_first name_last                                 address
1         1     Nutmeg  Nutmouse 14 Rose Cottage St.\nKenning UK, 323232
2         2     Tumtum  Nutmouse 14 Rose Cottage Blvd.\nKenning UK 34243
3         3     Marcus      Wood          243 Hill St.\nGuthrie OK 73402
4         4      Trudy       DAG          342 Elm\nDuncanville TX, 75116
5         5   John Lee    Walker      Hotel Suite\nNew Orleans LA, 70115
       telephone               email        dob age sex
1 (405) 321-1111     nutty@mouse.com 2003-08-30  11   0
2 (405) 321-2222    tummy@mouse.comm 2003-03-10  11   1
3 (405) 321-3333        mw@mwood.net 1934-04-09  80   1
4 (405) 321-4444 peroxide@blonde.com 1952-11-02  61   0
5 (405) 321-5555  left@hippocket.com 1955-04-15  59   1
  demographics_complete height weight   bmi
1                     2   7.00      1 204.1
2                     2   6.00      1 277.8
3                     2 180.00     80  24.7
4                     2 165.00     54  19.8
5                     2 193.04    104  27.9
                                                                                                     comments
1                                                                     Character in a book, with some guessing
2                                                                          A mouse character from a good book
3                                                                                          completely made up
4 This record doesn't have a DAG assigned\n\nSo call up Trudy on the telephone\nSend her a letter in the mail
5                 Had a hand for trouble and a eye for cash\n\nHe had a gold watch chain and a black mustache
     mugshot health_complete race___1 race___2 race___3 race___4 race___5
1 [document]               1        0        0        0        0        1
2 [document]               0        0        0        1        0        1
3 [document]               2        0        0        0        1        1
4 [document]               2        0        1        0        0        1
5 [document]               0        1        0        0        0        0
  race___6 ethnicity race_and_ethnicity_complete
1        0         1                           2
2        0         1                           0
3        0         0                           2
4        0         1                           2
5        1         2                           2

Read a subset of the records.

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.

The data dictionary describing 16 fields was read from REDCap in 1.1 seconds.  The http status code was 200.
2 records and 1 columns were read from REDCap in 0.5 seconds.  The http status code was 200.
Starting to read 2 records  at 2019-09-22 17:46:25.
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.5 seconds.  The http status code was 200.
The data dictionary describing 16 fields was read from REDCap in 0.2 seconds.  The http status code was 200.
2 records and 1 columns were read from REDCap in 0.5 seconds.  The http status code was 200.
Starting to read 2 records  at 2019-09-22 17:46:26.
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.
  record_id name_first name_last                                 address
1         1     Nutmeg  Nutmouse 14 Rose Cottage St.\nKenning UK, 323232
2         3     Marcus      Wood          243 Hill St.\nGuthrie OK 73402
       telephone           email        dob age sex demographics_complete
1 (405) 321-1111 nutty@mouse.com 2003-08-30  11   0                     2
2 (405) 321-3333    mw@mwood.net 1934-04-09  80   1                     2
  height weight   bmi                                comments    mugshot
1      7      1 204.1 Character in a book, with some guessing [document]
2    180     80  24.7                      completely made up [document]
  health_complete race___1 race___2 race___3 race___4 race___5 race___6
1               1        0        0        0        0        1        0
2               2        0        0        0        1        1        0
  ethnicity race_and_ethnicity_complete
1         1                           2
2         0                           2

Read a subset of the fields.

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 (i.e., fields) to the format required by the API (ie, fields_collapsed) if fields is specified and fields_collapsed is not.

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.4 seconds.  The http status code was 200.
Starting to read 5 records  at 2019-09-22 17:46:29.
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.
The data dictionary describing 16 fields was read from REDCap in 0.7 seconds.  The http status code was 200.
5 records and 1 columns were read from REDCap in 0.8 seconds.  The http status code was 200.
Starting to read 5 records  at 2019-09-22 17:46:31.
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.
  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

Read a subset of records, conditioned on the values in some variables.

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.

The data dictionary describing 16 fields was read from REDCap in 1 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 2019-09-22 17:46:33.
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.5 seconds.  The http status code was 200.
  record_id        dob weight
1         1 2003-08-30      1
2         2 2003-03-10      1
3         3 1934-04-09     80
4         4 1952-11-02     54
5         5 1955-04-15    104
[1] 3 5
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.7 seconds.  The http status code was 200.
Starting to read 2 records  at 2019-09-22 17:46:36.
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.
  record_id name_first name_last                            address
1         3     Marcus      Wood     243 Hill St.\nGuthrie OK 73402
2         5   John Lee    Walker Hotel Suite\nNew Orleans LA, 70115
       telephone              email        dob age sex
1 (405) 321-3333       mw@mwood.net 1934-04-09  80   1
2 (405) 321-5555 left@hippocket.com 1955-04-15  59   1
  demographics_complete height weight  bmi
1                     2 180.00     80 24.7
2                     2 193.04    104 27.9
                                                                                     comments
1                                                                          completely made up
2 Had a hand for trouble and a eye for cash\n\nHe had a gold watch chain and a black mustache
     mugshot health_complete race___1 race___2 race___3 race___4 race___5
1 [document]               2        0        0        0        1        1
2 [document]               0        1        0        0        0        0
  race___6 ethnicity race_and_ethnicity_complete
1        0         0                           2
2        1         2                           2

Additional Returned Information

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:

  1. The data object has the data.frame, as in the previous examples.
  2. The success boolean value indicates if redcap_read_oneshot believes the operation completed as intended.
  3. The status_codes is a collection of http status codes, separated by semicolons. There is one code for each batch attempted.
  4. The 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.
  5. The records_collapsed field passed to the API. This shows which record subsets, if any, were requested.
  6. The fields_collapsed fields passed to the API. This shows which field subsets, if any, were requested.
  7. The elapsed_seconds measures the duration of the call.
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.5 seconds.  The http status code was 200.
Starting to read 5 records  at 2019-09-22 17:46:37.
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.5 seconds.  The http status code was 200.
$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.5 seconds.  The http status code was 200."

$records_collapsed
[1] ""

$fields_collapsed
[1] "record_id,name_first,age"

$forms_collapsed
[1] ""

$events_collapsed
[1] ""

$filter_logic
[1] ""

$elapsed_seconds
[1] 1.948678

Session Information

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.6.1 (2019-07-05)
 os       Ubuntu 18.04.3 LTS          
 system   x86_64, linux-gnu           
 ui       X11                         
 language (EN)                        
 collate  C                           
 ctype    en_US.UTF-8                 
 tz       America/Chicago             
 date     2019-09-22                  

─ Packages ──────────────────────────────────────────────────────────────
 package     * version    date       lib source                         
 assertthat    0.2.1      2019-03-21 [3] CRAN (R 3.6.0)                 
 backports     1.1.4      2019-04-10 [3] CRAN (R 3.6.0)                 
 callr         3.3.1      2019-07-18 [3] CRAN (R 3.6.1)                 
 checkmate     1.9.4      2019-07-04 [3] CRAN (R 3.6.0)                 
 cli           1.1.0      2019-03-19 [3] CRAN (R 3.6.0)                 
 colorspace    1.4-1      2019-03-18 [3] CRAN (R 3.6.0)                 
 crayon        1.3.4      2017-09-16 [3] CRAN (R 3.6.0)                 
 curl          4.1        2019-09-16 [3] CRAN (R 3.6.1)                 
 desc          1.2.0      2018-05-01 [3] CRAN (R 3.6.0)                 
 devtools      2.2.0.9000 2019-09-21 [3] Github (r-lib/devtools@2765fbe)
 digest        0.6.21     2019-09-20 [3] CRAN (R 3.6.1)                 
 dplyr         0.8.3      2019-07-04 [3] CRAN (R 3.6.0)                 
 ellipsis      0.3.0      2019-09-20 [3] CRAN (R 3.6.1)                 
 evaluate      0.14       2019-05-28 [3] CRAN (R 3.6.0)                 
 fs            1.3.1      2019-05-06 [3] CRAN (R 3.6.0)                 
 glue          1.3.1      2019-03-12 [3] CRAN (R 3.6.0)                 
 hms           0.5.1      2019-08-23 [3] CRAN (R 3.6.1)                 
 htmltools     0.3.6      2017-04-28 [3] CRAN (R 3.6.0)                 
 httr          1.4.1      2019-08-05 [3] CRAN (R 3.6.1)                 
 kableExtra    1.1.0.9001 2019-05-18 [3] local                          
 knitr       * 1.25       2019-09-18 [3] CRAN (R 3.6.1)                 
 magrittr    * 1.5        2014-11-22 [3] CRAN (R 3.6.0)                 
 memoise       1.1.0      2017-04-21 [3] CRAN (R 3.6.0)                 
 munsell       0.5.0      2018-06-12 [3] CRAN (R 3.6.0)                 
 pillar        1.4.2      2019-06-29 [3] CRAN (R 3.6.0)                 
 pkgbuild      1.0.5      2019-08-26 [3] CRAN (R 3.6.1)                 
 pkgconfig     2.0.2      2018-08-16 [3] CRAN (R 3.6.0)                 
 pkgload       1.0.2      2018-10-29 [3] CRAN (R 3.6.0)                 
 prettyunits   1.0.2      2015-07-13 [3] CRAN (R 3.6.0)                 
 processx      3.4.1      2019-07-18 [3] CRAN (R 3.6.1)                 
 ps            1.3.0      2018-12-21 [3] CRAN (R 3.6.0)                 
 purrr         0.3.2      2019-03-15 [3] CRAN (R 3.6.0)                 
 R6            2.4.0      2019-02-14 [3] CRAN (R 3.6.0)                 
 Rcpp          1.0.2      2019-07-25 [3] CRAN (R 3.6.1)                 
 readr         1.3.1      2018-12-21 [3] CRAN (R 3.6.0)                 
 REDCapR     * 0.10.2     2019-09-22 [1] local                          
 remotes       2.1.0      2019-06-24 [3] CRAN (R 3.6.0)                 
 rlang         0.4.0      2019-06-25 [3] CRAN (R 3.6.0)                 
 rmarkdown     1.15       2019-08-21 [3] CRAN (R 3.6.1)                 
 rprojroot     1.3-2      2018-01-03 [3] CRAN (R 3.6.0)                 
 rstudioapi    0.10       2019-03-19 [3] CRAN (R 3.6.0)                 
 rvest         0.3.4      2019-05-15 [3] CRAN (R 3.6.0)                 
 scales        1.0.0      2018-08-09 [3] CRAN (R 3.6.0)                 
 sessioninfo   1.1.1      2018-11-05 [3] CRAN (R 3.6.0)                 
 stringi       1.4.3      2019-03-12 [3] CRAN (R 3.6.0)                 
 stringr       1.4.0      2019-02-10 [3] CRAN (R 3.6.0)                 
 testthat      2.2.1      2019-07-25 [3] CRAN (R 3.6.1)                 
 tibble        2.1.3      2019-06-06 [3] CRAN (R 3.6.0)                 
 tidyselect    0.2.5      2018-10-11 [3] CRAN (R 3.6.0)                 
 usethis       1.5.1      2019-07-04 [3] CRAN (R 3.6.0)                 
 vctrs         0.2.0      2019-07-05 [3] CRAN (R 3.6.0)                 
 viridisLite   0.3.0      2018-02-01 [3] CRAN (R 3.6.0)                 
 webshot       0.5.1      2018-09-28 [3] CRAN (R 3.6.0)                 
 withr         2.1.2      2018-03-15 [3] CRAN (R 3.6.0)                 
 xfun          0.9        2019-08-21 [3] CRAN (R 3.6.1)                 
 xml2          1.2.2      2019-08-09 [3] CRAN (R 3.6.1)                 
 yaml          2.2.0      2018-07-25 [3] CRAN (R 3.6.0)                 
 zeallot       0.1.0      2018-01-28 [3] CRAN (R 3.6.0)                 

[1] /tmp/Rtmp8vaOvg/Rinst10c579b5cb6d
[2] /tmp/RtmpsgrWgH/temp_libpath7402255995b5
[3] /home/wibeasley/R/x86_64-pc-linux-gnu-library/3.6
[4] /usr/local/lib/R/site-library
[5] /usr/lib/R/site-library
[6] /usr/lib/R/library

Report rendered by wibeasley at 2019-09-22, 17:46 -0500 in 20 seconds.