Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a script find itself in R running from the command line?

I have a script (call it Main.R) that has the following code to find itself when I run it:

frame_files <- lapply(sys.frames(), function(x) x$ofile)
frame_files <- Filter(Negate(is.null), frame_files) 
main.dir <- dirname(dirname(frame_files[[length(frame_files)]]))

This is used to get the directory above its own directory, main.dir, which is used to call other scripts relative to this path.

I'm interested in running this script from a command line, for example

R CMD BATCH Main.R

or

Rscript Main.R

Unfortunately, the commands above do not work when I call the script from the command line.

Is there any code I could put in Main.R or a call option to R or Rscript that I can use instead?

More specifically, the solution would need to work in Windows.

like image 686
J4y Avatar asked Nov 20 '12 16:11

J4y


2 Answers

Below is a solution that will give you the correct file directory path when the script is run either with source or with Rscript.

# this is wrapped in a tryCatch. The first expression works when source executes, the
# second expression works when R CMD does it.
full.fpath <- tryCatch(normalizePath(parent.frame(2)$ofile),  # works when using source
               error=function(e) # works when using R CMD
                     normalizePath(unlist(strsplit(commandArgs()[grep('^--file=', commandArgs())], '='))[2]))
dirname(full.fpath)

The key to this is the function normalizePath. Given a relative or abbreviated path name, normalizePath will return a valid path or raise an error. When running the script from Rscript, if you give normalizePath the base filename of the current script, it'll return the fullpath, regardless of what your current directory is. It even gets the path right when you supply a relative path to R CMD and there's a script with the same name in the current directory!

In the code above, I extract the filename from one of the strings returned by commandArgs. If you take a look at the output of commandArgs, you'll see that the filename is the 4th argument. The argument is recorded as '--file=yourscript.R', so in the final line above, I split the string on '=' and pull out the file name.

like image 53
Matthew Plourde Avatar answered Oct 08 '22 06:10

Matthew Plourde


On idea is to give the path as an argument to your Main.R

I Suppose You call it with RScript.

Rscript Main.R 'path' 

in your Main.R you add the code to read the argument

args <- commandArgs(trailingOnly = TRUE)
mainpath <- as.character(args[1])  
like image 1
agstudy Avatar answered Oct 08 '22 07:10

agstudy