Possible Duplicate:
Rscript: Determine path of the executing script
Always I run an R script, it take My Documents
as working directory.
When I run the script, I want to get the script path and change the working directory to it. How I can do this?
I want this to run the scripts in same folder with the source()
function.
If we want to check the current directory of the R script, we can use getwd( ) function. For getwd( ), no need to pass any parameters. If we run this function we will get the current working directory or current path of the R script. To change the current working directory we need to use a function called setwd( ).
You can use getwd() (to find the current path of the R process) or file. choose() to interactively find the file (it will return a character of the full path to the file). If you don't want to learn from this and/or do it with different files all the time, then you can short-circuit it with read.
In R, the function file. path() is used to fill in the directory separator. It knows which separator to use for the operating system it is running on.
if you run script by source
, then try source(file, chdir = TRUE)
.
What the hex, I try putting it in as a solution:
args <- commandArgs(trailingOnly = F)
scriptPath <- dirname(sub("--file=","",args[grep("--file",args)]))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With