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
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.
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.
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.
You can use :
source("script.R", chdir = TRUE)
and change "script.R" by the name of the file you're interested in.
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.
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