R/connect_to_procxed.R

Defines functions connect_to_procxed

Documented in connect_to_procxed

#' Open connection to ProcXed server
#'
#' Connect to s0260a\\dbxed to download ProcXed data cuts
#'
#' @param uid user ID
#'
#' @return an ODBC connection object
#' @export
#'
#' @examples
#' \dontrun{
#' connect_to_procxed("U123456")
#'}
connect_to_procxed <- function(uid, .in_func = FALSE) {

  # Clean up inputs -----------------------------------------------------------
  uid <- uid %>%
    as.character() %>%
    stringr::str_to_lower()

  ## Check UID is correct format
  if (!grepl("u[0-9]{6}", uid)) {
    stop("Please enter your user ID in the format 'u999999'.")
  }

  # Open connection to s0260a\dbxed -------------------------------------------
  odbc::dbConnect(
    drv = odbc::odbc(),
    .connection_string = paste0(
      "Driver={SQL Server};
       SERVER=s0260a\\dbxed;
       UID=", uid, ";
       DATABASE=char_and_ref;
       Trusted_Connection=Yes"
    )
  )

  # Remember to close the connection when you're done!
  if (!.in_func) {
    message(paste("Don't forget to close the connection using",
                "DBI::dbDisconnect([connection_name])."))
  }
}
n-fanton/ndr documentation built on Dec. 21, 2021, 11:07 p.m.