Skip to content

__FILE__ object in R

2 messages · Peter Waltman, Duncan Murdoch

#
On 12-09-21 3:27 PM, Peter Waltman wrote:
Those both lead to R being run with the command line option --file=<your 
script>.  As far as I can see, R doesn't save the filename in that case, 
it just opens the file and starts reading from it as though you were 
entering those lines in the console, but it does save the command line, 
so you can parse it yourself.  For example,

-----------------------------------------
#!/usr/bin/Rscript

getFilename <- function() {
   args <- commandArgs()
   filearg <- grep("^--file=", args, value=TRUE)
   if (length(filearg))
     sub("^--file=", "", filearg)
   else
     invisible(NULL)
}

getFilename()
-----------------------------------------

Duncan Murdoch