How to use exportRecordsTyped function from the redcapAPI package to import the data without factors?

224 Views Asked by At

I had previously used the following code to import data from REDCap:

data.df <- redcapAPI::exportRecords(rcon, 
                             forms = c("setup_log"),
                             labels = FALSE, 
                             factors = FALSE)

Because the exportRecords function is deprecated, I would like to use the new exportRecordsTyped function from the redcapAPI package. However, it seems to import all the strings as factors, which I do not want: the rest of my code relies on them not being factors.

I have been looking through the help page for exportRecordsTyped, but I have not found a way to import all the columns not as factors. I would like something like factors = FALSE, which does not seem to exist in the new exportRecordsTyped. Is there a way that I can do this?

I have found a way to do this with the REDCapR package redcap_read_oneshot function as follows, but I am still wondering if there is a way to do this with exportRecordsTyped.

data.df <- REDCapR::redcap_read_oneshot(Redcap.url,
                                        Redcap.token,
                                        forms = c("setup_log"),
                                        verbose = FALSE
                                        )$data 

Thanks.

2

There are 2 best solutions below

1
Till On BEST ANSWER

There is an alternative package for the redcap API: REDCapR. Its function redcap_read_oneshot() does not convert strings to factors.

rc_data <- 
  redcap_read_oneshot(
  redcap_uri = "https://<url/ip to your redcap instance here>/api/",
  token = "your redcap API key here")

rc_data$data
0
Benjamin On

UPDATE

with the release of version 2.8.0, this can be accomplished by using

exportRecordsTyped(rcon, 
                   cast = default_cast_no_factor)

Previous answer

Some options:

Post Processing

The fastest way to get what you want is probably

X <- exportRecordsTyped(rcon)

factor_cols <- vapply(X, is.factor, logical(1))
X[factor_cols] <- lapply(X[factor_cols], as.character)

Another alternative would be to use recastRecords

X <- exportRecordsTyped(rcon) |>
  recastRecords(rcon, 
             cast = list(dropdown = as.character, 
                         radio = as.character, 
                         checkbox = as.character, 
                         yesno = as.character))

but that's a lot of typing if you need to do that regularly.

Define your own casting functions

You can also define some overrides that return the character vectors instead of the factors.

castLabelCharacter <- function(x, field_name, coding){
  code_match <- getCodingIndex(x, coding)
  
  unname(coding[code_match])
}

castCheckedCharacter <- function(x, field_name, coding){
  checked_value <- getCheckedValue(coding, field_name)
  
  x_checked <- x %in% checked_value 
  
  c("Unchecked", "Checked")[(x_checked)+1]
}

And then

exportRecordsTyped(rcon, 
    cast = list(dropdown = castLabelCharacter, 
                 radio = castLabelCharacter, 
                 checkbox = castCheckedCharacter)

Redefine the provided casting functions

Lastly, you can change the definition of the casting functions before calling exportRecordsTyped

castLabel <- function(x, field_name, coding){
  code_match <- getCodingIndex(x, coding)
  
  unname(coding[code_match])
}

castChecked <- function(x, field_name, coding){
  checked_value <- getCheckedValue(coding, field_name)
  
  x_checked <- x %in% checked_value 
  
  c("Unchecked", "Checked")[(x_checked)+1]
}

This overwrites the behavior of the casting functions within your session and would need to be run each time you load redcapAPI.