Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@vm-wylbur
Created January 1, 2019 00:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vm-wylbur/98732f2e199097eee3956e6999578f3e to your computer and use it in GitHub Desktop.
Save vm-wylbur/98732f2e199097eee3956e6999578f3e to your computer and use it in GitHub Desktop.
get name of the running script in R (or RStudio)
.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