I have a R script that should change the working directory using setwd().
My code:
setwd(file.path(work_dir, database_dir, study))
I can pause my script just before this line is supposed to execute (using browser()
) enter the code into the console without issue, but whenever the line is executed from the script I get the error box stating that there is an "error while opening file: The system cannot find the file specified." Within my script before calling the setwd, I confirmed that the path exists using file.exists(file.path(work_dir, database_dir,study))
. I also restarted R studio and closed and reopened all of my scripts to make sure that was not causing the problem.
Does anyone have any other suggestions for me to try?
Posting how I resolved the issue:
The script that changed the directory (I will call this script 2) was called by another script first (I will call this script 1). script 1 and script 2 are in the same directory, but different directories from where I where I was trying to change. script 1 called script 2 using source("script2.R"). When I changed this to source(file.path(work_dir, "script2.R")) I no longer had issues with the setwd() command. My GUESS is that when I changed directories Rstudio "forgot" what script it was supposed to be executing and that is why I got a "no file exists" error.
If anyone has any other thoughts - please share!