R/utils.R

Defines functions data_path skip_if_no_auth `%||%` tipnet_token

tipnet_token <- function() {
  val <- Sys.getenv("REDCAP_TIPNET_PAT")
  if (identical(val, "")) {
    stop("`REDCAP_TIPNET_PAT` env var has not been set")
  }
  val
}


`%||%` <- function(x, y) if (is.null(x)) y else x


skip_if_no_auth <- function() {
  if (identical(Sys.getenv("REDCAP_TIPNET_PAT"), "")) {
    testthat::skip("No authentication available")
  }
}

#' Path to data folder
#' @param path path from which to start looking for the data
#' @export
data_path <- function(path = here::here()) {
  current_folder <- basename(normalizePath(path))
  path_to_data <- switch(current_folder,
    "TIPNet"       = fs::path(path, "..", "..", "tipnet-data"),
    "tipnet.ubesp" = fs::path(path, "..", "tipnet-data"),
    "report"       = data_path(fs::path(path, "..", "..", "tipnet-data")),
    "static"       = data_path(fs::path(path, "..", "..", "tipnet-data")),
    current_folder
  )

  if (path_to_data == current_folder) stop(current_folder)

  fs::dir_create(path_to_data)
}
UBESP-DCTV/tipnet.ubesp documentation built on April 12, 2025, 5:46 a.m.