Skip to content

Instantly share code, notes, and snippets.

@zekrom-vale
Last active November 23, 2021 22:04
Show Gist options
  • Save zekrom-vale/dbdce1e1339586b153a33eaa1cba50df to your computer and use it in GitHub Desktop.
Save zekrom-vale/dbdce1e1339586b153a33eaa1cba50df to your computer and use it in GitHub Desktop.
Computes the full or relative path of a Windows Explorer path with environment variables
library(glue)
#' Computes the full or relative path of a Windows Explorer path with environment variables
#' This makes it compatable to be used in any R path function
#' Ignores case and surounding whitespace
#'
#' @param path The Windows Explorer path
#' @param .open The start of the environment variable
#' @param .close The end of the environment variable
#' @return A full or relative path as text
#'
#' @examples
#' glue_env("% UsErPrOfIlE %/path")
#' glue_env("`userprofile`/path", "`", "`")
glue_env=function(path, .open="%", .close="%"){
env=list2env(as.list(Sys.getenv()))
path%>%
glue(
.open = .open,
.close = .close,
.envir = env,
.transformer = function(text, envir){
base::get(
text%>%
str_to_upper()%>%
str_trim(),
envir = envir
)
}
)
}
@zekrom-vale
Copy link
Author

If you need to use code in the path you can always pass it through glue after it is passed through glue_env. The issue with modifying glue_env's transformer to use the glue::identity_transformer(), is that everything will be converted to uppercase and will not work as intended. getwd() -> GETWD()

"%userprofile%{getwd()}"%>%
glue_env()%>%
glue()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment