REDCapR: we’ve used this in the past when we couldn’t get what we needed from REDCapAPI
For a comparison of different packages, see here (note: this might be biased as it was written by the REDCapAPI package developers)
2. Sending data into REDCap
library(redcapAPI)# set up connection ----url <-""token <-""con <-redcapConnection(url = url, token = token) # send data into REDCap ----importRecords(con,data = ed_data,overwriteBehavior ="normal",returnContent ="count")
url: you can find this in the sample code in the API Playground application in REDCap
token: you generate this from the API application in REDCap
2. Getting data out of REDCap
library(redcapAPI)# set up connection ----url <-""token <-""con <-redcapConnection(url = url, token = token) # get all fields for all records ----ed_census <-exportRecordsTyped(con)# get all fields from a certain form ----ed_census_form <-exportRecordsTyped(rcon = con,forms ="dsat")# get only certain fields ----ed_census_fields <-exportRecordsTyped(rcon = con,fields =c("record_id", "date", "edvol_tot"))
Storing data in REDCap
3. R Markdown
R Markdown and Quarto allow you to render R code into a document (PDF, HTML, Word, PPT, etc)
You can render an R Markdown or Quarto file from another script and specify where you want the file to be saved
Note - you render Quarto docs with quarto_render()
3. R Markdown
library(ggplot2)colors <-c(" ED Census"="blue", " Normal Range Limits"="yellow", "Danger Zone Limits"="red")edvol %>%ggplot(aes(x = date)) +geom_line(aes(y = high_thresh3, color ="Danger Zone Limits"), size = line_width) +geom_line(aes(y = high_thresh2, color =" Normal Range Limits"), size = line_width) +geom_line(aes(y = edvol_tot, color =" ED Census"), size = line_width) +geom_point(aes(y = edvol_tot), color ="blue", size =1, shape =15) +geom_line(aes(y = low_thresh2, color =" Normal Range Limits"), size = line_width) +geom_line(aes(y = low_thresh3, color ="Danger Zone Limits"), size = line_width) +ggtitle("ED Census") +xlab("Date") +ylab("Daily Census") +labs(subtitle =paste0(subtitle_dates, "\n", "San Mateo County Faciities Only"),colour ="",caption =" The limits of the normal range are determined by adding and subtracting twice the standard deviation from the average ED count for each month. The limits of the 'Danger Zone' are determined by adding and subtracting three times the standard deviation from the average ED count for each month.") +theme_minimal() +theme(plot.title =element_text(hjust =0.5, face ="bold", size =9),plot.subtitle =element_text(hjust =0.5, size =8),plot.caption =element_text(hjust =0.5, size =6),axis.title =element_text(face ="bold", size =8),axis.text.x =element_text(size =7),axis.text.y =element_text(size =7),legend.text =element_text(size =7),legend.position ="top") +scale_color_manual(values = colors)
3. R Markdown
4. Automated email
To make things as easy as possible for the end user, the last step of the R script sends an email with all the information and attaches the PDF rendered in step 3
If the API to get the data is down and/or if data are missing and need to be entered manually, the person sending the report will manually enter data in REDCap and render the PDF themselves
This is a huge pro of storing the data in REDCap instead of in an Excel or database without a front end
Lessons learned
Lessons learned
When choosing workflows to automate, it’s best to choose a mature workflow with defined business rules
Depending on your workflow, you might want to save a copy of the data you pull for record keeping
If you send an email, include explanations of the workflow to foster trust among end-users
Questions
Additional resources
Additional resources
There are some additional resources on the .README in GitHub. The resources include guides and additional information for: