How to get the script path in R? [duplicate]
Asked Answered
W

2

6

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.

Wilhelm answered 10/11, 2011 at 23:14 Comment(2)
If you use ESS there's a macro to do this. Probably for other editors too.Gerson
Perhaps: args <- commandArgs(trailingOnly = F) ; scriptPath <- dirname(sub("--file=","",args[grep("--file",args)])) From one of the answers in the above cited SO link.Rigmarole
A
12

if you run script by source, then try source(file, chdir = TRUE).

Appliance answered 11/11, 2011 at 0:4 Comment(0)
R
2

What the hex, I try putting it in as a solution:

args <- commandArgs(trailingOnly = F)
 scriptPath <- dirname(sub("--file=","",args[grep("--file",args)]))
Rigmarole answered 11/11, 2011 at 2:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.