Download a file from a REDCap project record.

Share:

Description

This function uses REDCap's API to download a file

Usage

1
2
3
redcap_download_file_oneshot(file_name = NULL, directory = NULL,
  overwrite = FALSE, redcap_uri, token, record, field, event = "",
  verbose = TRUE, config_options = NULL)

Arguments

file_name

The name of the file where the downloaded file is saved. If empty the original name of the file will be used and saved in the default directory. Optional.

directory

The directory where the file is saved. By default current directory. Optional

overwrite

Boolean value indicating if existing files should be overwritten. Optional

redcap_uri

The URI (uniform resource identifier) of the REDCap project. Required.

token

The user-specific string that serves as the password for a project. Required.

record

The record ID where the file is to be imported. Required

field

The name of the field where the file is saved in REDCap. Required

event

The name of the event where the file is saved in REDCap. Optional

verbose

A boolean value indicating if messages should be printed to the R console during the operation. Optional.

config_options

A list of options to pass to POST method in the httr package. See the details below. Optional.

Details

Currently, the function doesn't modify any variable types to conform to REDCap's supported variables. See validate_for_write for a helper function that checks for some common important conflicts.

Value

Currently, a list is returned with the following elements,

  1. success: A boolean value indicating if the operation was apparently successful.

  2. status_code: The http status code of the operation.

  3. outcome_message: A human readable string indicating the operation's outcome.

  4. records_affected_count: The number of records inserted or updated.

  5. affected_ids: The subject IDs of the inserted or updated records.

  6. elapsed_seconds: The duration of the function.

  7. raw_text: If an operation is NOT successful, the text returned by REDCap. If an operation is successful, the 'raw_text' is returned as an empty string to save RAM.

  8. file_name: The name of the file persisted to disk. This is useful if the name stored in REDCap is used (which is the default).

Author(s)

Will Beasley

John J. Aponte

References

The official documentation can be found on the ‘API Help Page' and 'API Examples’ pages on the REDCap wiki (ie, https://community.projectredcap.org/articles/456/api-documentation.html and https://community.projectredcap.org/articles/462/api-examples.html). If you do not have an account for the wiki, please ask your campus REDCap administrator to send you the static material.

The official cURL site discusses the process of using SSL to verify the server being connected to.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
## Not run:   
uri     <- "https://bbmc.ouhsc.edu/redcap/api/"
token   <- "D70F9ACD1EDD6F151C6EA78683944E98" #pid=213
record  <- 1
field   <- "mugshot"
# event <- "" # only for longitudinal events

result_1 <- redcap_download_file_oneshot(
  record=record, field=field, 
  redcap_uri=uri, token=token
)
base::unlink("mugshot-1.jpg")

(full_name <- base::tempfile(pattern="mugshot", fileext=".jpg"))
result_2 <- redcap_download_file_oneshot(
  file_name=full_name, record=record, field=field, 
  redcap_uri=uri, token=token
)
base::unlink(full_name)

(relative_name <- "ssss.jpg")
result_3 <- redcap_download_file_oneshot(
  file_name=relative_name, record=record, field=field, 
  redcap_uri=uri, token=token
)
base::unlink(relative_name)

## End(Not run)