R/sys_script.R

Defines functions sys.script

Documented in sys.script

## http://stackoverflow.com/questions/1815606/rscript-determine-path-of-the-executing-script#comment12780420_1815606

#' Get path of current script
#'
#' @export
#'
#' @source
#' http://stackoverflow.com/questions/1815606/rscript-determine-path-of-the-executing-script
#'
#' @examples \dontrun{
#' ## cat foo.R
#' ## library(funr);sys.script()
#'
#' ## source("foo.R")
#'
#' ## Rscript foo.R
#'
#' }
sys.script <- function(){
	cmdargs <- commandArgs(trailingOnly = FALSE)
	fl <- grep("--file=", cmdargs)
	if (length(fl) > 0) {
		# Rscript
		return(normalizePath(gsub("--file=", "", cmdargs[fl])))
	} else {
		# 'source'd via R console
		return(normalizePath(sys.frames()[[1]]$ofile))
	}
}

Try the funr package in your browser

Any scripts or data that you put into this service are public.

funr documentation built on May 1, 2019, 9:02 p.m.