I understand this is outdated, but I couldn't get the former answers to work very satisfactorily, so I wanted to contribute my method in case any one else encounters the same error mentioned in the comments to BumbleBee's answer.
Mine is based on a simple system command. All you feed the function is the name of your script:
extractRootDir <- function(x) {
abs <- suppressWarnings(system(paste("find ./ -name",x), wait=T, intern=T, ignore.stderr=T))[1];
path <- paste("~",substr(abs, 3, length(strsplit(abs,"")[[1]])),sep="");
ret <- gsub(x, "", path);
return(ret);
}
setwd(extractRootDir("myScript.R"));
The output from the function would look like "/Users/you/Path/To/Script"
. Hope this helps anyone else who may have gotten stuck.
?setwd
?getwd
– Neldaload
.rdata-files in the same folder, notsource
with the working directory set to the path of the sourced file. – Selfliquidating