Skip to content

Instantly share code, notes, and snippets.

@wch
Created April 11, 2019 00:09
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 wch/57ee974b2b0b4251ee3ef16345cc31f0 to your computer and use it in GitHub Desktop.
Save wch/57ee974b2b0b4251ee3ef16345cc31f0 to your computer and use it in GitHub Desktop.
Function for detecting the script that's being run
# This script can be sourced from RStudio, or run with Rscript.
# Returns the file currently being sourced or run with Rscript
this_file <- function() {
cmdArgs <- commandArgs(trailingOnly = FALSE)
needle <- "--file="
match <- grep(needle, cmdArgs)
if (length(match) > 0) {
# Rscript
return(normalizePath(sub(needle, "", cmdArgs[match])))
} else {
# 'source'd via R console
return(normalizePath(sys.frames()[[1]]$ofile))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment