REDCapCAST/R/easy_redcap.R

43 lines
1.1 KiB
R
Raw Permalink Normal View History

#' Retrieve project API key if stored, if not, set and retrieve
#'
#' @param key.name character vector of key name
#'
#' @return character vector
#' @importFrom keyring key_list key_get key_set
#' @export
get_api_key <- function(key.name) {
if (key.name %in% keyring::key_list()$service) {
keyring::key_get(service = key.name)
} else {
2024-02-06 14:00:26 +01:00
keyring::key_set(service = key.name, prompt = "Provide REDCap API key:")
keyring::key_get(service = key.name)
}
}
#' Secure API key storage and data acquisition in one
#'
#' @param project.name The name of the current project (for key storage with
2024-02-06 14:00:26 +01:00
#' `keyring::key_set()`, using the default keyring)
#' @param widen.data argument to widen the exported data
2024-02-05 14:46:20 +01:00
#' @param uri REDCap database API uri
#' @param ... arguments passed on to `REDCapCAST::read_redcap_tables()`
#'
#' @return data.frame or list depending on widen.data
#' @export
2024-02-27 13:20:21 +01:00
easy_redcap <- function(project.name, widen.data = TRUE, uri, ...) {
key <- get_api_key(key.name = paste0(project.name, "_REDCAP_API"))
out <- read_redcap_tables(
2024-02-05 14:46:20 +01:00
uri = uri,
2024-02-06 14:00:26 +01:00
token = key,
...
)
2024-02-27 13:20:21 +01:00
if (widen.data) {
2024-02-06 14:00:26 +01:00
out <- out |> redcap_wider()
}
out
}