Last active
November 23, 2021 22:04
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
) | |
} | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you need to use code in the path you can always pass it through
glue
after it is passed throughglue_env
. The issue with modifyingglue_env
's transformer to use theglue::identity_transformer()
, is that everything will be converted to uppercase and will not work as intended.getwd() -> GETWD()