Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

directory

path

r

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.

like image 827
Renato Dinhani Avatar asked Nov 10 '11 23:11

Renato Dinhani


People also ask

How do I get the path of a script in R?

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( ).

Where is the file path in RStudio?

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.

What does file path do in R?

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.


2 Answers

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

like image 70
kohske Avatar answered Oct 19 '22 03:10

kohske


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

args <- commandArgs(trailingOnly = F)
 scriptPath <- dirname(sub("--file=","",args[grep("--file",args)]))
like image 30
IRTFM Avatar answered Oct 19 '22 01:10

IRTFM