reflects changes to redcap_wider()

This commit is contained in:
Andreas Gammelgaard Damsbo 2024-02-06 14:00:26 +01:00
parent 0ce516c297
commit edd5334df8
2 changed files with 7 additions and 23 deletions

View File

@ -10,7 +10,7 @@ get_api_key <- function(key.name) {
if (key.name %in% keyring::key_list()$service) { if (key.name %in% keyring::key_list()$service) {
keyring::key_get(service = key.name) keyring::key_get(service = key.name)
} else { } else {
keyring::key_set(service = key.name, prompt = "Write REDCap API key:") keyring::key_set(service = key.name, prompt = "Provide REDCap API key:")
keyring::key_get(service = key.name) keyring::key_get(service = key.name)
} }
} }
@ -19,40 +19,24 @@ get_api_key <- function(key.name) {
#' Secure API key storage and data acquisition in one #' Secure API key storage and data acquisition in one
#' #'
#' @param project.name The name of the current project (for key storage with #' @param project.name The name of the current project (for key storage with
#' `keyring::key_set()`) #' `keyring::key_set()`, using the default keyring)
#' @param widen.data argument to widen the exported data #' @param widen.data argument to widen the exported data
#' @param uri REDCap database API uri #' @param uri REDCap database API uri
#' @param ... arguments passed on to `REDCapCAST::read_redcap_tables()` #' @param ... arguments passed on to `REDCapCAST::read_redcap_tables()`
#' #'
#' @return data.frame or list depending on widen.data #' @return data.frame or list depending on widen.data
#' @importFrom purrr reduce
#' @importFrom dplyr left_join
#' @export #' @export
easy_redcap <- function(project.name, widen.data = TRUE, uri, ...) { easy_redcap <- function(project.name, widen.data=TRUE, uri, ...) {
key <- get_api_key(key.name = paste0(project.name, "_REDCAP_API")) key <- get_api_key(key.name = paste0(project.name, "_REDCAP_API"))
out <- read_redcap_tables( out <- read_redcap_tables(
token = key,
uri = uri, uri = uri,
token = key,
... ...
) )
all_names <- out |> if (widen.data){
lapply(names) |> out <- out |> redcap_wider()
Reduce(c, x = _) |>
unique()
if (widen.data) {
if (!any(c("redcap_event_name", "redcap_repeat_instrument") %in%
all_names)) {
if (length(out) == 1) {
out <- out[[1]]
} else {
out <- out |> purrr::reduce(dplyr::left_join)
}
} else {
out <- out |> redcap_wider()
}
} }
out out

View File

@ -8,7 +8,7 @@ easy_redcap(project.name, widen.data = TRUE, uri, ...)
} }
\arguments{ \arguments{
\item{project.name}{The name of the current project (for key storage with \item{project.name}{The name of the current project (for key storage with
`keyring::key_set()`)} `keyring::key_set()`, using the default keyring)}
\item{widen.data}{argument to widen the exported data} \item{widen.data}{argument to widen the exported data}