Skip to content

Commit

Permalink
Merge pull request #557 from OuhscBbmc/dev
Browse files Browse the repository at this point in the history
add cron example
  • Loading branch information
wibeasley authored Dec 9, 2024
2 parents 064df5f + 34cf745 commit 02afc46
Show file tree
Hide file tree
Showing 3 changed files with 1,792 additions and 2 deletions.
14 changes: 12 additions & 2 deletions R/helpers-testing.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,23 @@ retrieve_credential_testing <- function(
if(Sys.getenv("redcapr_test_server") != "") {
Sys.getenv("redcapr_test_server")
} else {
# nocov start
message("The test server was not explicitly set, so defaulting to 'dev-2'.")
"dev-2"
# nocov end
}
checkmate::assert_character(server_instance , any.missing = FALSE, min.chars = 2, max.chars = 50)

# This line avoids a warning from the package check.
projects <- project_id <- instance <- tag <- NULL

if (!requireNamespace("yaml", quietly = TRUE)) {
# nocov start
stop(
"Package `yaml` must be installed to use this function.",
call. = FALSE
)
# nocov end
}
d_map <-
system.file("misc/project-redirection.yml", package = "REDCapR") |>
Expand All @@ -40,18 +44,20 @@ retrieve_credential_testing <- function(
dplyr::filter(tag == project_tag)

if (nrow(d_map) == 0L) {
stop("A credential mapping entry does not exist for the desired arguments.")
stop("A credential mapping entry does not exist for the desired arguments.") # nocov
}

path_credential <- system.file(d_map$credential_file, package = "REDCapR")
if (!base::file.exists(path_credential)) {
# nocov start
stop(
"The credential file `",
d_map$credential_file,
"` associated with the `",
server_instance,
"` does not exist on this machine."
)
# nocov end
}

retrieve_credential_local(
Expand All @@ -67,19 +73,23 @@ retrieve_plugins <- function(plugin_name) {
if(Sys.getenv("redcapr_test_server") != "") {
Sys.getenv("redcapr_test_server")
} else {
# nocov start
message("The test server was not explicitly set for plugins, so defaulting to 'dev-2'.")
"dev-2"
# nocov end
}
checkmate::assert_character(server_instance , any.missing = FALSE, min.chars = 2, max.chars = 50)

# This line avoids a warning from the package check.
plugins <- instance <- tag <- project_tag <- NULL

if (!requireNamespace("yaml", quietly = TRUE)) {
# nocov start
stop(
"Package `yaml` must be installed to use this function.",
call. = FALSE
)
# nocov end
}
d_map <-
system.file("misc/plugin-redirection.yml", package = "REDCapR") |>
Expand All @@ -98,7 +108,7 @@ retrieve_plugins <- function(plugin_name) {
dplyr::filter(tag == plugin_name)

if (nrow(d_map) == 0L) {
stop("A plugin mapping entry does not exist for the desired arguments.")
stop("A plugin mapping entry does not exist for the desired arguments.") # nocov
}

d_map |>
Expand Down
28 changes: 28 additions & 0 deletions utility/cron-example.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Simplified script incorporating a cron job & REDCapR
# Adapted from https://heds.nz/posts/automate-r-reporting-linux-cron/
message("==============================")
message(paste0(Sys.time(), " Starting cron job...\n"))

uri <- "https://redcap-dev-2.ouhsc.edu/redcap/api/"

message("---- simple project --------------------------")
token_1 <- "9A068C425B1341D69E83064A2D273A70"
REDCapR::redcap_metadata_read(redcap_uri=uri, token=token_1)

message("---- repeating measures project w/ environmental token --------------------------")
Sys.setenv(REDCAP_KIRA_SGM_KEY = "77842BD8C18D3408819A21DD0154CCF4")
token_3 <- Sys.getenv("REDCAP_KIRA_SGM_KEY")
REDCapR::redcap_metadata_read(redcap_uri=uri, token=token_3)

message("---- longitudinal project w/ stored token--------------------------")
path <- system.file("misc/dev-2.credentials", package = "REDCapR")
# message(path)
message("Credential file exists: ", fs::file_exists(path))
credential <- REDCapR::retrieve_credential_local(path, 34L)
# message(credential)
REDCapR::redcap_metadata_read(redcap_uri=credential$redcap_uri, token=credential$token)

message("------------------------------")
message(paste0(Sys.time(), " Finished running utility/cron-example.R.\n"))

# * * * * * Rscript ~/redcap/REDCapR/utility/cron-example.R >> ~/redcap/REDCapR/utility/cron-example.log 2>&1
Loading

0 comments on commit 02afc46

Please sign in to comment.