Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command for "Set working directory to source file location"

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

like image 501
Alex Coppock Avatar asked Oct 23 '14 00:10

Alex Coppock


People also ask

How do I set the source directory in R?

Step 1) Click on the Session tab. Step 2) Click on Set Working Directory > To Source File Location. Afterwards, you working directory will be changed to the location of your source file.

What does the Getwd () command do?

getwd(): The getwd() method is used to gather information about the current working pathname or default working directory. This function has no arguments. It returns an absolute pathname. It returns NULL in case there is not any working directory.

How do I change the working directory of a file?

Answer: Use the cd Command The current working directory is the directory or folder where you are currently working. You can use the cd (change directory) command to change the current working directory or move around the file system. This command will work in all Linux distribution.


2 Answers

You can use :

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

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

like image 158
Romain Avatar answered Oct 12 '22 12:10

Romain


pathwd<-sub("/filename","",system("find -perm -g=w -type f -name 'filename'",intern=T)[1])
setwd(pathwd)

Make sure your file's name is unique.

like image 1
Ciro Avatar answered Oct 12 '22 13:10

Ciro