Try using parent.frame(3)
in you function:
setwd_thisdir <- function () {
this.dir <- dirname(parent.frame(3)$ofile)
setwd(this.dir)
}
See ?parent.frame
or http://adv-r.had.co.nz/Environments.html#calling-environments.
You may also look at the chdir
option of the source
function (?source
).