-
-
Save couthcommander/e379c7c2c91a40e529052574dd12475c to your computer and use it in GitHub Desktop.
fileRepository API functionality
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(redcapAPI) | |
key <- '' | |
rcon <- redcapConnection("https://redcap.vanderbilt.edu/api/", key, conn, 'MatchedRandomization1') | |
fr_ls <- function(rcon, folder_id = NA, recursive = FALSE) { | |
args <- list(rcon = rcon, content = 'fileRepository', action = 'list', format = 'csv') | |
if(!is.na(folder_id)) { | |
args$folder_id <- folder_id | |
parent <- folder_id | |
} else { | |
parent <- 'top-level' | |
} | |
# need to call once, and convert to DF if any data | |
has_res <- nchar(do.call(genericApiCall, c(args, make_data_frame = FALSE))) > 0 | |
if(!has_res) return(NULL) | |
root <- do.call(genericApiCall, c(args, make_data_frame = TRUE)) | |
root <- cbind(parent_folder = parent, root) | |
if(recursive) { | |
fids <- root[!is.na(root[,'folder_id']), 'folder_id'] | |
if(length(fids)) { | |
addl <- do.call(rbind, lapply(fids, fr_ls, rcon = rcon, recursive = TRUE)) | |
root <- rbind(root, addl) | |
} | |
} | |
root | |
} | |
myfile <- '~/Downloads/tennessee-history.csv' | |
fr_ls(rcon, recursive = TRUE) | |
genericApiCall(rcon, content='fileRepository', action='createFolder', make_data_frame = FALSE, name = 'rcode') | |
tmp <- fr_ls(rcon) | |
rcode_id <- tmp[tmp[,'name'] == 'rcode','folder_id'] | |
genericApiCall(rcon, content='fileRepository', action='createFolder', folder_id=rcode_id, make_data_frame = FALSE, name = 'check') | |
tmp <- fr_ls(rcon, folder_id=rcode_id) | |
check_id <- tmp[tmp[,'name'] == 'check','folder_id'] | |
fh <- httr::upload_file(myfile) | |
genericApiCall(rcon, content='fileRepository', action='import', folder_id=check_id, make_data_frame = FALSE, file=fh) | |
tmp <- fr_ls(rcon, recursive = TRUE) | |
file_id <- tmp[tmp[,'name'] == basename(myfile),'doc_id'] | |
data <- genericApiCall(rcon, content='fileRepository', action='export', doc_id=file_id) | |
genericApiCall(rcon, content='fileRepository', action='delete', doc_id=file_id, make_data_frame = FALSE) | |
## you can't delete a directory with the API! | |
# genericApiCall(rcon, content='fileRepository', action='delete', folder_id=check_id, make_data_frame = FALSE) | |
fr_ls(rcon, recursive = TRUE) |
On another note, I have begun incorporating this into redcapAPI. I've borrowed heavily from your approach here. Would it be okay with you if I proposed adding you as a contributor?
Sounds good, thanks for implementing!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Under vubiostat/redcapAPI#28, we may be removing genericApiCall in the near future. It is being replaced with makeApiCall. I think the following would be the correct update to this gist.
https://gist.github.com/nutterb/bc6ecbe51a1c54cb94ed1731112c1929