redcapAPI
redcapAPI copied to clipboard
Import number values 1 decimal with trailing zeros like 5.0 gives error "does not follow the expected format"
Thanks for this REDCap API interface library for R, it's proven pretty useful already. At the moment we're converting research data from an older system to REDCap projects. We have created a REDCap dictionary based on the old system and exported the data as csv files.
Instead of importing the files one-by-one through the REDCap web user interface (too many files, too slow, freezes etc) we're trying to import the data using R scripts. This works for most files, except it doesn't work for certain decimal values. Specifically, for columns that contain numeric 1 decimal values with trailing zeros, we get this error:
The value you provided could not be validated because it does not follow the expected format. Please try again.
This only happens for variable when Validation = Number 1 decimal
, and the values contain trailing zeros like 5.0
10.0
etc, but there is no error message for other decimal values like 2.5
9.3
etc. See the code below, it reads a CSV file with data, builds a dataframe and send the dataframe to the Redcap API.
library(redcapAPI)
library(dplyr)
# Set workdirectory
setwd('C:/Users/XxxxxX/source/ConvertStudydata')
# config API-token + REDCap url
api_url = "https://redcap.xxxxxxxx.xxx/redcap/api/"
api_token = "1234567890ABCDEF1234567890ABCDEF"
# Setup API Connection
rconnection <- redcapConnection(url = api_url, token = api_token)
recordId_colClasses <- c("studynr"="character", "mutation_date"="Date")
csvALGEMEEN <- read.csv('./Data/Import_ALGEMEEN.csv', sep = ',', dec='.', fileEncoding = 'UTF-8-BOM', colClasses = recordId_colClasses)
dfALGEMEEN <- importRecords(rconnection, csvALGEMEEN, returnData = FALSE, logfile = "import_logfile_ALGEMEEN.txt")
The csv file contains values like 20.0
, 5.0
, 10.0
etc. and importing it into a dataframe in RStudio using read.csv(..
the dataframe still contains the correct values, see screenshot.
However, when I try to import the dataframe using the importRecords
function I get the error message:
dfALGEMEEN <- importRecords(rconnection, csvALGEMEEN, returnData = FALSE, logfile = "import_logfile_ALGEMEEN.txt") Error in redcap_error(x, error_handling = "error") : 400: ERROR: "20240012","rookaant","10","The value you provided could not be validated because it does not follow the expected format. Please try again." "20240014","rookaant","20","The value you provided could not be validated because it does not follow the expected format. Please try again." "20240017","rookaant","5","The value you provided could not be validated because it does not follow the expected format. Please try again."
The error message only appears for the values ending on .0
decimals, it doesn't give any error on the values 8.1
or 5.2
etc. I also tried to force the CSV reader to interpret the column as character
but then I still get the same error when importing. The variable in REDCap is defined as "Number (1 decimal)", see screenshot below:
When I import the same file in the REDCap web gui then I don't get the error message. Any idea how to import these decimal values using R and the API library? Is this a bug in the library maybe, or are there extra parameter I need to set? fyi I'm using REDCap v12.4.6 and Rstudio 2022.07.0 Build 548