Commit 86358c0d authored by Jonathan Mang's avatar Jonathan Mang 💡
Browse files

feat: enhanced `clean_path_name` and `get_config_env`

Added support to also remove the tailing slash in paths, added support of extracting also variables from the environment that aren't previously set
parent e3164794
Pipeline #21895 passed with stages
in 21 minutes and 32 seconds
Package: DIZutils
Title: Utilities for 'DIZ' R Package Development
Version: 0.0.6.9003
Date: 2021-01-05
Version: 0.0.6.9004
Date: 2021-01-08
Authors@R:
c(person(given = "Jonathan M.",
family = "Mang",
......
#' @title clean_path_name helper function
#'
#' @description Function to clean paths to have a tailing slash
#' @description Function to clean paths to surely have a tailing slash or not
#'
#' @param pathname A character string. A path name to be cleaned
#' (to have a tailing slash).
#' (to have a tailing slash or not).
#' @param remove.slash (boolean) Default: FALSE. Should the result
#' contain the tailing slash or remove it?
#' @return The result is the input but with an tailing slash.
#'
#'
......@@ -14,6 +16,10 @@
#'
#' @export
#'
clean_path_name <- function(pathname) {
return(gsub("([[:alnum:]])$", "\\1/", pathname))
clean_path_name <- function(pathname, remove.slash = FALSE) {
if (remove.slash) {
return(gsub("\\/$", "", pathname))
} else {
return(gsub("([[:alnum:]])$", "\\1/", pathname))
}
}
#' @title get_config_env helper function
#'
#' @description Internal function to read settings for a certain system
#' from the environment
#' from the environment.
#' IMPORTANT: If yout want to get any result with your input as prefix,
#' use `ignore.presets = TRUE`! See param-definition for more details.
#'
#' @param system_name The name of the system (This is also the prefix used
#' to get the environment variables with 'SYSTEM_KEY', e.g. 'I2B2_DBNAME')
#' @param ignore.presets (boolean) Only return something if all elements
#' from the presets are found? These are currently `host`, `port`, `user`,
#' `password`, `sid`, `path`. If you have another suffix after
#' `system_name` in your config file, you won't see it here. To see
#' everything with prefix `system_name` simply set `ignore.presets = TRUE`.
#'
#' @inheritParams db_connection
#' @return If successful it returns the config, null otherwise.
......@@ -20,28 +27,47 @@
#'
get_config_env <-
function(system_name,
logfile_dir,
headless) {
logfile_dir = tempdir(),
headless = TRUE,
ignore.presets = FALSE) {
res <- tryCatch({
system_name <- toupper(system_name)
elements <- list(
dbname = "_DBNAME"
, host = "_HOST"
, port = "_PORT"
, user = "_USER"
, password = "_PASSWORD"
, sid = "_SID"
, path = "_PATH"
)
if (ignore.presets) {
env_names_tmp <- names(Sys.getenv())
elements <-
env_names_tmp[grepl(pattern = paste0("^", system_name, "_*"),
x = env_names_tmp)]
# Get the environment variable for each all keys:
res <- lapply(
X = elements,
FUN = function(name) {
tmp <- Sys.getenv(paste0(system_name, name))
return(tmp)
}
)
# Get the environment variable for these elements:
res <- sapply(
X = elements,
FUN = function(name) {
tmp <- list()
tmp[name] = Sys.getenv(name)
return(Sys.getenv(name))
}
)
res <- as.list(res)
} else {
elements <- list(
dbname = "_DBNAME",
host = "_HOST",
port = "_PORT",
user = "_USER",
password = "_PASSWORD",
sid = "_SID",
path = "_PATH"
)
# Get the environment variable for each all keys:
res <- lapply(
X = elements,
FUN = function(name) {
tmp <- Sys.getenv(paste0(system_name, name))
return(tmp)
}
)
}
# Now remove the empty entries:
res <- res[lapply(res, nchar) > 0]
......
......@@ -26,7 +26,10 @@ my_desc$set_authors(c(
role = c("aut"),
comment = c(ORCID = "0000-0003-1866-860X")
),
person("MIRACUM - Medical Informatics in Research and Care in University Medicine", role = c("fnd")),
person(
"MIRACUM - Medical Informatics in Research and Care in University Medicine",
role = c("fnd")
),
person("Universitätsklinikum Erlangen", role = "cph")
))
......@@ -34,7 +37,7 @@ my_desc$set_authors(c(
my_desc$del("Maintainer")
# Set the version
my_desc$set_version("0.0.6.9003")
my_desc$set_version("0.0.6.9004")
# The title of your package
my_desc$set(Title = "Utilities for 'DIZ' R Package Development")
......@@ -59,7 +62,8 @@ my_desc$set("Language" = "en-US")
# The urls
my_desc$set("URL", "https://github.com/miracum/misc-dizutils")
my_desc$set("BugReports", "https://github.com/miracum/misc-dizutils/issues")
my_desc$set("BugReports",
"https://github.com/miracum/misc-dizutils/issues")
# Vignette Builder
# my_desc$set("VignetteBuilder" = "knitr")
......@@ -68,13 +72,14 @@ my_desc$set("BugReports", "https://github.com/miracum/misc-dizutils/issues")
my_desc$set("License", "GPL-3")
# System requirements (for CRAN to avoid Rpostgres-errors):
my_desc$set("SystemRequirements", "libpq >= 9.0: libpq-dev (deb) or postgresql-devel (rpm)")
my_desc$set("SystemRequirements",
"libpq >= 9.0: libpq-dev (deb) or postgresql-devel (rpm)")
# Save everyting
my_desc$write(file = "DESCRIPTION")
# License
usethis::use_gpl3_license(name="Universitätsklinikum Erlangen")
usethis::use_gpl3_license(name = "Universitätsklinikum Erlangen")
# add Imports and Depends
......@@ -98,7 +103,7 @@ usethis::use_package("data.table", type = "Imports")
# usethis::use_package("ggpubr", type = "Imports")
# usethis::use_package("magrittr", type = "Imports")
# usethis::use_package("polynom", type = "Imports")
usethis::use_package("DBI", type="Imports", min_version = "1.1.0")
usethis::use_package("DBI", type = "Imports", min_version = "1.1.0")
usethis::use_package("RJDBC", type = "Imports")
usethis::use_package("RPostgres", type = "Imports")
usethis::use_package("shiny", type = "Imports")
......
......@@ -4,17 +4,20 @@
\alias{clean_path_name}
\title{clean_path_name helper function}
\usage{
clean_path_name(pathname)
clean_path_name(pathname, remove.slash = FALSE)
}
\arguments{
\item{pathname}{A character string. A path name to be cleaned
(to have a tailing slash).}
(to have a tailing slash or not).}
\item{remove.slash}{(boolean) Default: FALSE. Should the result
contain the tailing slash or remove it?}
}
\value{
The result is the input but with an tailing slash.
}
\description{
Function to clean paths to have a tailing slash
Function to clean paths to surely have a tailing slash or not
}
\examples{
# Both function calls will return "home/test/"
......
......@@ -4,7 +4,12 @@
\alias{get_config_env}
\title{get_config_env helper function}
\usage{
get_config_env(system_name, logfile_dir, headless)
get_config_env(
system_name,
logfile_dir = tempdir(),
headless = TRUE,
ignore.presets = FALSE
)
}
\arguments{
\item{system_name}{The name of the system (This is also the prefix used
......@@ -16,13 +21,21 @@ The absolute path to folder where the logfile will be stored.}
\item{headless}{A boolean (default: FALSE). Indicating, if the function is
run only in the console (headless = TRUE) or on a GUI frontend
(headless = FALSE).}
\item{ignore.presets}{(boolean) Only return something if all elements
from the presets are found? These are currently `host`, `port`, `user`,
`password`, `sid`, `path`. If you have another suffix after
`system_name` in your config file, you won't see it here. To see
everything with prefix `system_name` simply set `ignore.presets = TRUE`.}
}
\value{
If successful it returns the config, null otherwise.
}
\description{
Internal function to read settings for a certain system
from the environment
from the environment.
IMPORTANT: If yout want to get any result with your input as prefix,
use `ignore.presets = TRUE`! See param-definition for more details.
}
\examples{
\donttest{
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment