Using R and REDCap API to automate export/import dataset

I am working on the database design in REDCap for a cooperative NIH U grant and one functionality of this database we want to build is to track the bio-sample (plasma, DNA…)location (in transit, Lab1, Lab2…). It’s like the UPS tracking system where one can get real time update of the sample location.

The idea is simple, we build 2 projects in REDCap. The main project contains the unique sample label information, which is entered when the sample is collected. The second project will serve the tracking purpose with ID field and a list of possible location/status of the sample. We would like the label from main project to be autopopulated into project 2 as the record ID so the RA can update the status when changes happen.

The REDCap API and the brilliant R developers made things possible.

There are quite a few posts introducing how to export data from REDCap using R packages such as RCurl and REDCapR, but rarely I find information on how to import back to a different project after we are done with data manipulation.

The purpose of this post is to introduce the package  redcapAPI for automating export and import dataset (talk between REDCap projects).

First of all, you need to request 2 tokens for your REDCap admin team, one for each project. Let’s call them token_ex and token_im.

The tokens serve as unique security ID for you to get data in and out of REDCap project. Once you have the tokens for your export and import project, you can follow the example code to run the API.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
library(redcapAPI)
redcap_url <- "https://redcap.nubic.northwestern.edu/redcap/api/"
token_ex <- "yourmainproject"
 
rcon <- redcapConnection(url=redcap_url, token=token_ex)
bd <- exportRecords(rcon, forms="blood_draw") # only export the blood draw form
bd1 <- bd[,c("mdnalbl","pllbl","plm0lbl","plm0_1lbl","blood_draw_complete")] 
# save the 4 labels and the form status into a dataframe
bd2 <- bd1[bd1$blood_draw_complete==2& !is.na(bd1$blood_draw_complete),c(c("mdnalbl","pllbl","plm0lbl","plm0_1lbl"))] 
# filter out non-complete data
d_import <- bd2[!is.na(bd2)]
# empty labels will be removed 
d_import <- data.frame(d_import) 
# our labels ready to be imported into project 2
names(d_import)="record_id" 
# becomes the record id in project2
token_im="yourtrackingproject"
rconim=redcapConnection(url=redcap_url,token=token_im)
importRecords(rconim,data=d_import,overwriteBehavior = c("normal", "overwrite"),
              returnContent = c("count", "ids", "nothing"), returnData = FALSE,
              logfile = "")

If you want to schedule to run this script daily/weekly… you can setup scheduled task in Windows/Mac easily.

Posted in All, R.

Leave a Reply

Your email address will not be published. Required fields are marked *