0

I'm using R to query from a redcap project. My code looks like this:

token <- "123456789123456789"
url <- "https://redcap.hospitalname.org/redcap/api/"
dataFromRedcap<-redcap_read(redcap_uri = url,token = token, export_survey_fields = TRUE)
data<-dataFromRedcap$data

Now, I use similar code practically everyday and it works fine. For one particular project, the query keeps failing though.

enter image description here

The record status dashboard looks like this, and I suspect the fact that the record_id column is text names instead of "1","2","3", etc... is probably causing the issue. It says "Personnel Name" there, but in the codebook that is the "record_id" variable. enter image description here

I've tested my theory by attempting to query other projects designed similarly (record_id is character) and it'll fail, while working on "normal" projects.

Now, first and foremost, I'm not sure why they coded the record_id's that way, I never would have done it, but it is what it is.

Is there a way for redcap_read to still pull the records?

Joe Crozier
  • 944
  • 8
  • 20
  • You can try 2 things to get more details on what is failing. Try recap_read() with these arguments 1) `continue_on_error=TRUE`. 2) `verbose=T`. What do you get? – MendelYev Aug 01 '23 at 15:31
  • Continue_on_error=TRUE still fails, pretty much just gives the same error as above but twice (one for each batch). Verbose=T gives the exact same error as above, no new information. Literally word for word same error. – Joe Crozier Aug 01 '23 at 15:42
  • It seems to be an error on RedCap side rather than the R package. I have found the message `The returned dataset is empty` [here](https://github.com/OuhscBbmc/REDCapR/blob/34f2154852fb52fb99bccd8e8295df8171eb1c18/R/redcap-read-oneshot.R#L372) just after a failing call to [RedCap API](https://github.com/OuhscBbmc/REDCapR/blob/34f2154852fb52fb99bccd8e8295df8171eb1c18/R/redcap-read-oneshot.R#L311). It looks like the message is shown if RedCap returns an empty content. – MendelYev Aug 01 '23 at 16:09
  • Oh I absolutely agree its on the redcap side. I think its specifically because of the custom naming of the record_id's using character text rather than the normal numeric values. My question is if there's a way to circumvent that. I know redcap_read has an argument for "id_position" to choose the column position that uniquely identifies the subject if its not record_id. The problem is it IS "record_id" in our case, its just not a usual format. – Joe Crozier Aug 01 '23 at 16:14
  • I don't have a RedCap instance running to check it myself, but you should be able to call the API directly from RedCap web ui. I think that it will return a more usefull error message on how to solve this. Original error message is not displayed from R package. From RecCap, could you go to API Playground and try API Method = Export Records, Format=CSV, Type=flat and look for a hint in the error message? – MendelYev Aug 01 '23 at 18:27
  • When I go to the API playground and use their suggested R code to export, it works just fine. No error at all. My question was more pertaining to redcap_read specifically. More of a curiosity than anything. – Joe Crozier Aug 01 '23 at 18:42

1 Answers1

0

As discussed in comments: RedCap is providing a working R script for your use case in its API Playground section.

If you want to know the exact reason redcap_read is failing, you should probably get the package sources and pause its execution right after the API call to analyze the error RedCap returned (before the generic error shown by the package).

MendelYev
  • 144
  • 7
  • I dont feel this answers the original text of my question "Is there a way for redcap_read to still pull the records?" My question was not "get data from redcap by any means necessary". As a result I will not be accepting this solution. I'll defer to Stack Overflow mods etc if I need to revise my question or something – Joe Crozier Aug 01 '23 at 19:05
  • 1
    No problem :) Sorry no time to explore in details the root cause.. – MendelYev Aug 01 '23 at 19:14
  • Yea I realized (via our conversation) that I actually just want to know more about redcap_read and/or let the package author know something may not be working. That reminded me that I've actually spoken with them before and so I just reached out. – Joe Crozier Aug 01 '23 at 19:17