Skip to content

Commit e0251e0

Browse files
committed
add get_redcap_credentials
1 parent 0850419 commit e0251e0

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export(get_institutional_person_data)
2424
export(get_job_duration)
2525
export(get_package_scope_var)
2626
export(get_project_life_cycle)
27+
export(get_redcap_credentials)
2728
export(get_redcap_db_connection)
2829
export(get_redcap_email_revisions)
2930
export(get_redcap_emails)

R/get_redcap_credentials.R

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#' Retrieve REDCap Credentials Based on Specified Parameters
2+
#'
3+
#' Fetches REDCap credentials from the CREDENTIALS_DB, allowing filtering based on
4+
#' project ID, server short name, project short name, and username. At least one filtering
5+
#' criterion must be provided.
6+
#'
7+
#' @param project_pid Optional project ID for filtering.
8+
#' @param server_short_name Optional server short name for filtering.
9+
#' @param project_short_name Optional project short name for filtering.
10+
#' @param username Optional username for filtering.
11+
#'
12+
#' @return A dataframe of filtered REDCap credentials, including a 'url' column added for convenience.
13+
#'
14+
#' @examples
15+
#' get_redcap_credentials(project_pid = "123")
16+
#' get_redcap_credentials(server_short_name = Sys.getenv("SERVER_SHORT_NAME"))
17+
#'
18+
#' @export
19+
#'
20+
get_redcap_credentials <- function(project_pid = NA,
21+
server_short_name = NA,
22+
project_short_name = NA,
23+
username = NA) {
24+
25+
# Verify that there is at least one parameter
26+
if (
27+
all(is.na(
28+
c(
29+
server_short_name,
30+
username,
31+
project_pid,
32+
project_short_name
33+
)
34+
))
35+
) {
36+
stop("At least one parameter must be defined")
37+
}
38+
39+
credentials_conn <- DBI::dbConnect(RSQLite::SQLite(), Sys.getenv("CREDENTIALS_DB"))
40+
41+
redcap_credentials <- dplyr::tbl(credentials_conn, "credentials") |>
42+
# Filter on any non-NA parameter
43+
# Parameters have to be localized so that will not be seen as columns in the data frame
44+
dplyr::filter(is.na(!!project_pid) | .data$project_id == !!project_pid) |>
45+
dplyr::filter(is.na(!!server_short_name) | .data$server_short_name == !!server_short_name) |>
46+
dplyr::filter(is.na(!!project_short_name) | .data$project_short_name == !!project_short_name) |>
47+
dplyr::filter(is.na(!!username) | .data$username == !!username) |>
48+
dplyr::collect() |>
49+
# Make a copy of redcap_uri to make redcapAPI coding a tiny bit simpler
50+
dplyr::mutate(url = redcap_uri)
51+
52+
DBI::dbDisconnect(credentials_conn)
53+
54+
return(redcap_credentials)
55+
}
56+

man/get_redcap_credentials.Rd

+35
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)