Created
January 1, 2019 00:13
-
-
Save vm-wylbur/98732f2e199097eee3956e6999578f3e to your computer and use it in GitHub Desktop.
get name of the running script in R (or RStudio)
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
.get_running_name <- function() { | |
#' returns name of current script | |
if (interactive()) { | |
fname <- rstudioapi::getActiveDocumentContext()$path | |
} else { | |
# via commandArgs, look for file= | |
cmdArgs = commandArgs(trailingOnly = FALSE) | |
needle <- '--file=' | |
fname <- grep(needle, cmdArgs, value=TRUE) | |
} | |
fname = tools::file_path_as_absolute(fname) | |
if (length(fname) > 0) { | |
fname <- basename(fname) | |
return(tools::file_path_sans_ext(basename(fname))) | |
} else { | |
stop("unable to determine currently running script. quitting.") | |
} | |
return(FALSE) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment