knitr::opts_chunk$set(collapse = FALSE)
library(collaborator); library(dplyr)

CollaboratoR: Group-specific emails (mailmerge in R)

In large-scale multicentre reseach, communication with data collectors in a meaningful way can be challenging. Often, group-specific emails (with group-specific attachments) can be desired (for example reports of missing data for a particular site). Yet there is a limited number of non-proprietary softwares that allow this to be automated at scale, and often this is required to be done on a manual basis.

CollaboratoR has several functions that have been designed to work together to faciliate the process of sending group-specific emails (including attachments). This has been developed with interoperability with REDCap in mind, but the "email_" functions do not require REDCap to work.

Why would you choose to do this over mailmerge or other equivalent software?

   

1. Build email dataset

The first step is to define the groups of people that will be emailed.

a). REDCap user export

For projects on REDCap, all users with access rights to each data access group (DAG) can be accessed via the API.

This can be done via the user_role() CollaboratoR function (see Redcap User Management: 1. Explore Current Users for more details), alongside several other functions from other packages ( redcapAPI / REDCapAPI / RCurl ).

df_user_all <- collaborator::user_role(redcap_project_uri = Sys.getenv("collaborator_test_uri"),
                                       redcap_project_token = Sys.getenv("collaborator_test_token"))$full

knitr::kable(head(df_user_all, n=10)) # Please note all names / emails have been randomly generated

However, these do not produce the correct data format as the data (email) must be summarised by group (data_access_group). This can be done directly via the user_summarise() function. This produces data in the exact format required by the subsequent "email_" functions (alongside some additional summarised data which may be of interest). This is the recommended option for handing REDCap user data for this purpose.

df_user <- collaborator::user_summarise(redcap_project_uri = Sys.getenv("collaborator_test_uri"),
                                        redcap_project_token = Sys.getenv("collaborator_test_token"),
                                        user_exclude = "y_o’doherty")

knitr::kable(df_user) # Please note all names have been randomly generated

 

b). Other sources

While these email functions were developed for the intent of integration with REDCap, the subsequent "email_" functions are build to not require REDCap to work. However, the same minimum input format is required:

For example (using the data above to illustrate):

df_user_other <- df_user %>%
  dplyr::select("group" = data_access_group, "group_specific_emails_recipients" = user_email)

  knitr::kable(df_user_other)

There may be any number of additional columns present that can be used to "mail-merge" within the email subject or body.

   

2. Build group-specific emails

At this stage, we have a dataframe of the grouped recipents of the emails. Now we can begin to build the group-specific components of the emails.

a). Generate email fields

The email_field() function wrangles the dataframe of grouped recipents (e.g. df_user above) into the format required by email_send(). There are two aspects of email fields that can be customised:

df_email <- collaborator::email_field(df_email = df_user,
                                      group = "data_access_group",
                                      recipient_main = NULL,
                                      recipient_cc = NULL,
                                      recipient_bcc = "user_email", # we want all the recipients to be "BCC". 
                                      subject = "Hello to [user_n] collaborators at [data_access_group]")

knitr::kable(df_email)

 

b). Add email body

The email_body() function will perform a mailmerge using a specified rmarkdown file (e.g. "vignette_email_body.Rmd") and the output from email_field(). This will form the email body to be sent via email_send().

df_email <- collaborator::email_body(df_email, 
                                     html_output = "code", 
                                     rmd_file = here::here("vignettes/vignette_email_body.Rmd"))

tibble::as_tibble(df_email)

 

c). Add email attachments

The group2csv() function will split a tibble/dataframe by "group" variable, then save grouped data in a subfolder as individual CSV files. This can be used as a group-specific attachment to be sent via email_send().

# Generate patient-level / anonomysed missing data report
report <- collaborator::report_miss(redcap_project_uri = Sys.getenv("collaborator_test_uri"),
                                    redcap_project_token = Sys.getenv("collaborator_test_token"))$record


attach <- collaborator::group2csv(data = report,
                                  group = "redcap_data_access_group",
                                  subfolder = here::here("vignettes/folder_csv"), file_prefix = "missing_data_")

knitr::kable(attach)

The output from group2csv() and any other attachments must be appended to the output from email_field() as additional columns.

df_email <- df_email %>%
  dplyr::left_join(attach, group=c("redcap_data_access_group" = "group")) %>%

  dplyr::mutate(file2 = here::here("man/figures/collaborator_logo.png"))

Note: Only group2csv() will be supported as a function for creating group-specific files en mass (e.g. rather than pdf, word, etc) as these are too heterogeneous and specific in their purpose. However, once created via rmarkdown, their pathways can be joined to the correct group and will be attached if included in the email_send() function.

   

3. Send group-specific emails

We now have created our four components to the group-specific email:

 

a). Gmail connection set-up

The email_send() function is currently built for use with gmail via the gmailr package. To function, a connection to the Gmail API must be established in advance (find step by step process here).

gmailr::gm_auth_configure(path = "gmail.json")

 

b). Sending

The email_send() will allow automated sending of the prepared group-specific emails and their attachments. The following parameters can be specified:

collaborator::email_send(df_email = df_email,
                         sender = "email@gmail.com", 
                         email_body = "code", 
                         attach = c("file", "file2"), zip = T,
                         draft = FALSE)

The email_send() function will print the number + group of emails as they are sent. This facilitates troubleshooting in the event of errors.

   



kamclean/collaborator documentation built on Nov. 17, 2023, 3:52 a.m.