Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.