redcapAPI - failing to overwrite file on file repository (R)

61 Views Asked by At

While I can get this script to work just fine (it imports a file into a file repository in a REDCap project), it fails to overwrite the existing file.

library(redcapAPI)

file <- 'C:/Temp/my_report.html'
rcon <- redcapConnection(token="xyz",url = 'https://redcap.project.website/api/')

importToFileRepository(
  rcon,
  file,
  folder_id = 1,
  overwrite = TRUE,
)

Am I missing something? I would think overwrite=TRUE would overwrite the file, but it does not.

1

There are 1 best solutions below

0
On

I can understand why you would want and/or expect this behavior. Unfortunately, the REDCap doesn't support an overwrite argument in its API method. Consequently, importToFileRepository doesn't support an overwrite argument.

It would be possible to mimic this behavior by writing a separate function that exports the contents of the File Repository folder, looks for a file of the same file name, and if one exists, captures the document ID to delete after importing the new file.

I'm pseudocoding this, so please don't expect it to work out of the box, but something like

importWithOverwrite <- function(rcon, file, folder_id, overwrite = FALSE, ...){
  if (overwrite){
    FileRepo <- exportFileRepositoryListing(rcon,
                                            folder_id = folder_id)
    current_doc_id <- FileRepo$doc_id[FileReport$name == basename(file)]
  }

  is_success <- importToFileRepository(rcon,
                                       file = file, 
                                       folder_id = folder_id, 
                                       ...)

  # Only delete the existing file if the import is successful first
  if (is_success && ovewrite){
    deleteFromFileRepository(rcon, 
                             doc_id = current_doc_id)
  }
}

file <- 'C:/Temp/my_report.html'
rcon <- redcapConnection(token="xyz",url = 'https://redcap.project.website/api/')

importWithOverwrite(rcon, 
                    file = file, 
                    folder_id = 1, 
                    overwrite = TRUE)

If you have a GitHub account, you can submit an issue at https://github.com/vubiostat/redcapAPI/issues. These kinds of needs are pretty warmly received among their group.