#' Open a connection to a PHS database
#'
#' @param dsn The Data Source Name passed on to `odbc::dbconnect`
#' the dsn must be setup first. e.g. SMRA or DVPROD
#' @param username The username to use for authentication,
#' if not supplied it first will check the environment variable
#' and finally ask the user for input.
#'
#' @return a connection to the specified dsn
#' @export
phs_database_conn <- function(dsn, username = Sys.getenv("USER")) {
# Collect username from the environment
username <- Sys.getenv("USER")
# Check the username is not empty and take input if not
if (is.na(username) | username == "") {
username <- rstudioapi::showPrompt(
title = "Username",
message = "Username",
default = ""
)
}
# Create the connection
password_text <- glue::glue("{dsn} password for user: {username}")
db_connection <- odbc::dbConnect(
odbc::odbc(),
dsn = dsn,
uid = username,
pwd = rstudioapi::askForPassword(password_text)
)
return(db_connection)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.