Reputation: 71
I face the problem with changing of working directory. Here are my steps to reproduce it:
This is serious problem if i work with folders which contain many files as new session just fails to start
Upvotes: 0
Views: 447
Reputation: 8318
You can use the following at the beginning of each script:
# set the R scripts working directory
library(rstudioapi)
current_path <- getActiveDocumentContext()$path
if (getwd() != current_path){
setwd(dirname(current_path ))
}
This piece of code will define the working directory as the location of the script, it's really useful when working with multiple scripts that has lots of dependencies and they fail due to wrong working directory
EDIT
After understanding better the desired behaviour from the comments, you should wrap you code in a project hierarchy and set the the default working directory of the project to the desired one.
The code I added above is suitable for the case you want your scripts to run regardless of the session working directory
Upvotes: 1