Skip to content

Instantly share code, notes, and snippets.

@lolow
Created September 7, 2016 15:07
Show Gist options
  • Save lolow/48ec8a443bb5002edf2e1a828fb3e58d to your computer and use it in GitHub Desktop.
Save lolow/48ec8a443bb5002edf2e1a828fb3e58d to your computer and use it in GitHub Desktop.
file_ <- function(){
for (i in -(1:sys.nframe())) {
if (identical(args(sys.function(i)), args(base::source)))
return(normalizePath(sys.frame(i)$ofile))
}
cmd_args <- commandArgs(trailingOnly = FALSE)
cmd_args_trailing <- commandArgs(trailingOnly = TRUE)
leading_idx <- seq.int(from = 1, length.out = length(cmd_args) -
length(cmd_args_trailing))
cmd_args <- cmd_args[leading_idx]
res <- gsub("^(?:--file=(.*)|.*)$", "\\1", cmd_args)
res <- tail(res[res != ""], 1)
if (length(res) > 0)
return(res)
}
@lolow
Copy link
Author

lolow commented Sep 7, 2016

return the path of the current script

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment