Command for "Set working directory to source file location"

Alex Coppock picture Alex Coppock · Oct 23, 2014 · Viewed 12k times · Source

RStudio has a useful feature:

Session -> Set Working Directory -> To Source File Location

Is there a way to do this without using the drop down menus?

UPDATE:

maybe a better way of asking is:

is there a command to return the file path of the current r script?

I also found this thread, but the solutions didn't work for me. Not even Hadley's!

Rscript: Determine path of the executing script

Answer

Romain picture Romain · Oct 23, 2014

You can use :

source("script.R", chdir = TRUE)

and change "script.R" by the name of the file you're interested in.