vubiostat / redcapAPI

R interface to REDCap (http://www.project-redcap.org/)
20 stars 26 forks source link

Data Query Support #321

Closed spgarbet closed 5 months ago

spgarbet commented 8 months ago

Data Query on Redcap: User Guide

Need to add support for 'Data Query' module.

spgarbet commented 7 months ago

Data Query is coming up in more meetings. I mentioned this ticket and we can support it with some development.

nutterb commented 7 months ago

Maybe I'm missing something, but can this actually be supported right now? I'm not seeing anything in the user guide you linked to that indicates the API can access these. I also can't find mention of it in the API documentation.

Could you elaborate on what support you hope to provide?

obregos commented 7 months ago

There is a module that can be added to REDCap by the DataCore team called Data Quality. It is not automatically available for every project. The Data Quality module gives users access to create/open and close queries on records. I included the rccola function to pull queries as an example below. We have old rccola code that pulls, builds, closes queries that I would like to integrate into redcapAPI.

# Pull Queries
cola.pull.queries.f <- function(pid, api_name){

  loadFromRedcap(
    c(api_name),
    FUN=function(key, ...){
      url <- paste0("https://redcap.vanderbilt.edu/api/?prefix=vanderbilt_dataQuality&page=export&type=module&NOAUTH&pid=", pid)

      formData <- list(
        token = key
      )

      response <- httr::POST(url, body = formData, encode = "form")
      result <- httr::content(response, type='application/json')
      return(result)
    }
  )

  result <- get(api_name)
  for(j in 1:length(result)){i=result[[j]];if(is.null(i$resolutions)){result[[j]]$resolutions=list()}}
  result <- as.data.frame(do.call(rbind, result))

  if(nrow(result)>0) {
    columns <- c("status_id", "project_id", "record", "event_id", "instance","field_name")
    for(c in columns){
      result[,c] <- unlist(result[,c])
    }

    result$codes <- lapply(result$resolutions, function(rs){
      rs_df <- as.data.frame(do.call(rbind, rs))
      m <- str_match(unlist(rs_df$comment),"^([a-zA-Z]+\\d+):\\s")[,2]
      m <- m[!is.na(m)]
      return(m)
    })
  }

  return(result)
}
nutterb commented 7 months ago

Cool! You're pulling it without using the API. I'm much less confused now.

spgarbet commented 7 months ago

That's an interesting URL that it hits.

prefix=vanderbilt_dataQuality implies that the module prefix would be different across institutions. Which then forces that to be a parameter.

This looks like only a pull, is there an API to push data into it?

Also it would be interesting to see if it could be retrieved as a csv as there are simple routines to convert that into R internal to redcapAPI without all the de-JSONing code in the example.

spgarbet commented 7 months ago

Maybe target this (assuming csv works). The pid argument is associated with the API_KEY, I don't know why that would be required.

exportDataQuality(rcon, prefix, api_param = list(), csv_delimiter = ",", ...)
spgarbet commented 5 months ago

Merged.