OuhscBbmc / REDCapR

R utilities for interacting with REDCap
https://ouhscbbmc.github.io/REDCapR
Other
112 stars 45 forks source link

REDCapR API `rename_record` to rename record_id is not supported? #513

Open BdR76 opened 8 months ago

BdR76 commented 8 months ago

In REDCap we've got a project with ~700 records which were initially imported about a year ago. However, they were imported with incorrectly formatted recordIDs, without leading zero's. So the record ids are for example 1, 2, 3 etc instead of 001, 002, 003. This wasn't a problem before but now we are trying to merge data with another system and it would really be easier if they're formatted correctly, so with the leading zeros.

REDCap has an API for renaming records, see <Your Redcap env>/redcap/api/help/?content=rename_record and this would be ideal for updating all the recordIDs using an R script. So read a csv and call the API instead of doing >700 records by hand in the REDCap web application.

However, as far as I can see this API is not available as a function in the REDCapR library, is that correct? I couldn't find the rename record function (possible there are other API functions missing as well)

(REDCapR v1.1.0)

pbchase commented 4 months ago

@BdR76, yes, I would love to see rename_record supported in REDCapR. Until that day, I offer this example of how to do it in the redcapAPI library.

library(tidyverse)
library(redcapAPI)

record_renames <- tribble(
  ~old, ~new,
  "1", "001",
  "2", "002",
  "3", "003",
)

# Rename record
rcapi_conn <- redcapAPI::redcapConnection(
  url = Sys.getenv("URI"),
  token = Sys.getenv("TOKEN")
)

result <- purrr::map2_lgl(
  record_renames$old,
  record_renames$new,
  ~ redcapAPI::renameRecord(rcapi_conn, .x, .y)
)

rename_result <- record_renames |>
  select(old, new) |>
  bind_cols(result = result)

It's a little clunky because the redcapAPI::renameRecord is not vectorized and the result is hard to interpret when the rename is partial. My hacks with purrr::map2_lgl and bind_cols ease those pain points.

spgarbet commented 2 weeks ago

Kudos on using ENV variables to keep your api key out of code. We recently added some ENV searching to unlockREDCap as well (now it works with ENV, yaml, and an interactive key locker).