Closed loremerdrignac closed 4 months ago
James Humphreys has set up the process to do this from RedCap in the WHO/EURO project. tokens currently stored on his computer. Next challenge is to devise secure way to store and share tokens.
Apologies I hadn't seen this issue previously. I have a (relatively) simple function for automated queries to RedCAP. It's currently set to my requirements, though I can generalise this script for use in other projects/Epiuf package:
runApi <- function(token) {
url <- "CENSORED REDCAP API URL"
## Parameters for the request - here you can refine which records you're interested in
formData <- list("token"=token,
content='record',
action='export',
format='csv',
type='flat',
csvDelimiter='',
rawOrLabel='raw',
rawOrLabelHeaders='raw',
exportCheckboxLabel='true',
exportSurveyFields='true',
exportDataAccessGroups='true',
returnFormat='csv'
)
## make the request using httr and POST method
response <- httr::POST(url, body = formData, encode = "form")
## convert the httr response content and assign it to the name 'df'
REDCapExtract <- httr::content(response)
dataLocation <- pathToFile("SOURCES", paste0(countryEq,"/", yearDate ," ", month_number))
fileName <- paste0(dataLocation,"CENSORED PROJECT FILE PATH",Sys.Date(),"_",strftime(Sys.time(), "%H%M"),".csv")
## download the r dataframe to CSV
write.csv(REDCapExtract, fileName, row.names=FALSE)
}
API_request <- function(updateDate, country_code) {
updateYearMonth <- strsplit(updateDate, "-")[[1]][1:2]
updateYearMonth <- gsub("_", "", updateYearMonth)
formattedUpdateDate <- paste(updateYearMonth, collapse = " ")
source_path <- pathToFile("SOURCES", paste0(country_code,"/",formattedUpdateDate))
filenames <- list.files(path = source_path)
# Check if any of the filenames contain the updateDate
file_exists <- any(grepl(updateDate, filenames))
if (file_exists) {
print("File exists. No API call was made.")
} else if (file.exists("C:/Users/james/Documents/Dev/R/GIT-R/local_configs/EuroConfigs/QueryRCAP_GENERALISED.R")) {
source("C:/Users/james/Documents/Dev/R/GIT-R/local_configs/EuroConfigs/QueryRCAP_GENERALISED.R")
} else {
tokenValue <- readline(prompt = "Please enter your API token for the survey of interest: ")
responseDataframe <- runApi(tokenValue)
}
}
2023-09-14 Meeting with JHS, JHD, GDS, LMC
You will find below a quick summary of our conclusion:
Note: Once the agreed solution will be integrated in James' script above, we will include it in the epiuf package to share with the other projects using REDCap.
Update 12/10/23:
On a weekly basis, data extraction from RedCp and KoboToolBox is done manually whereas an API exists and could be called from R. Need to use .Renviron to store RedCap token (See Jenny for more tech information available on RedCap and KoboToolBox website)