ctsit / redcapcustodian

Simplified, automated data management on REDCap systems
Other
13 stars 6 forks source link

Write `update_redcap_email_addresses()` #11

Closed pbchase closed 2 years ago

pbchase commented 2 years ago

Write update_redcap_email_addresses(). It will look mostly like this:

update_redcap_email_addresses <- function(con, redcap_email_revisions) {
  # break down updated emails by field
  # columns (e.g. user_email<n>) cannot be parameterized
  # pivot_wider cannot be used as NAs in non-replacement fields result in overwrites
  # the solution is to create a list of lists, one list for each email_field_name
  redcap_email_change_groups <- redcap_email_revisions %>%
    select(email_field_name, corrected_email, ui_id) %>%
    group_split(email_field_name, .key = "email_field_name", .keep = F) %>%
    # RMySQL prepared statements do not allow named parameters
    lapply(as.list) %>%
    lapply(unname) %>%
    setNames(nm = c(paste0("user_email", seq_along(.))))

  # Write corrected emails to redcap db, erasing where necessary
  dbExecute(
    con,
    paste0(
      "UPDATE redcap_user_information ",
      "SET user_email = ? ",
      "WHERE ui_id = ?"
    ),
    redcap_email_change_groups$user_email1
  )

  dbExecute(
    con,
    paste0(
      "UPDATE redcap_user_information ",
      "SET user_email2 = ? ",
      "WHERE ui_id = ?"
    ),
    redcap_email_change_groups$user_email2
  )

  dbExecute(
    con,
    paste0(
      "UPDATE redcap_user_information ",
      "SET user_email3 = ? ",
      "WHERE ui_id = ?"
    ),
    redcap_email_change_groups$user_email3
  )
}

Do these things:

  1. Add the function as a redcapcustodian package function
  2. Qualify function calls with library names
  3. Add ROxygen docs, examples, tests, etc.
ljwoodley commented 2 years ago

It seems that redcap_email_revisions originates from lines 123-169 in https://gist.github.com/pbchase/faef5bde7bc6e705dbf93e71aa1d6f5a#file-clean_up_bad_email_addresses-r-L123.

I don't have access to bad emails returned from get_bad_emails_from_listserv_digest and am confused as to the contents of this df. I'll also need this info to create a test for the function.

pbchase commented 2 years ago

This is dependent of PR #31. @ChemiKyle , please review that PR :-)

ChemiKyle commented 2 years ago

Closed by #36