Created
April 11, 2019 00:09
-
-
Save wch/57ee974b2b0b4251ee3ef16345cc31f0 to your computer and use it in GitHub Desktop.
Function for detecting the script that's being run
This file contains hidden or 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
# 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