readline_retry <- function(text, retries = 2, validator) {
if (missing(validator) || is.null(validator))
validator <- function(value) TRUE
retried <- 0
output <- readline(text)
while (!validator(output) & (retries - retried) > 0) {
retried <- retried + 1
output <- readline(text)
}
if (!validator(output)) {
message(sprintf("Giving up after %d retries, returning NULL.\n", retries))
output <- NULL
}
output
}
tarpan_config_file <- function() {
if (isTRUE(nchar(Sys.getenv("TARPAN_CONFIG")) > 0))
Sys.getenv("TARPAN_CONFIG")
else
"~/.tarpan_readonly_config"
}
.onAttach <- function(libname, pkgname) {
config_file_path <- tarpan_config_file()
if (file.exists(config_file_path)) {
tarpan_load_config()
Sys.chmod(config_file_path, "0700")
}
if (interactive()) {
if (!file.exists(config_file_path) ||
!any(c("user", "password") %in% names(fromJSON(config_file_path)))) {
cache_creds <- readline_retry(
text = "Cache database username and password? (y\\n) ",
retries = 1,
validator = function(value) isTRUE(tolower(value) %in% c("y", "n"))
)
if (identical(cache_creds, "y")) {
set_tarpan_credentials()
} else {
packageStartupMessage(
"You can always set your Tarpan with set_tarpan_credentials"
)
}
} else
packageStartupMessage("Credentials found at ", config_file_path)
}
packageStartupMessage("Welcome to taRpan!")
}
env <- environment()
.onLoad <- function(libname, pkgname) {
finalizer <- function(x) {
x
tarpan2_disconnect()
}
reg.finalizer(env, finalizer, onexit = TRUE)
}
.onUnload <- function(libpath) {
tarpan2_disconnect()
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.