How can a source
d or Sweave
d file find out its own path?
Background:
I work a lot with .R scripts or .Rnw files. My projects are o
This answer works for source
and also inside nvim-R
- I have no idea if it works with knitr
and similar things. Any feedback appreciated.
If you have multiple scripts source
-ing each other, it is important to get the correct one. That is, the largest i
for which sys.frame(i)$ofile
exists.
get.full.path.to.this.sourced.script = function() {
for(i in sys.nframe():1) { # Go through all the call frames,
# in *reverse* order.
x = sys.frame(i)$ofile
if(!is.null(x)) # if $ofile exists,
return(normalizePath(x)) # then return the full absolute path
}
}