In many (interpreted) programming languages, a variable is set when sourcing a file so that some code can determine the filename from where it is loaded. E.g. in ruby, the variable __FILE__ is set when loading a file.
Is there such a feature in R? Is there a way for R code to determine from where it is loaded?
Example:
main.R:
source("foo.R")
/home/bar/foo.R:
print(FULL_FILENAME)
What do I have to replace FULL_FILENAME with to make it print:
[1] "/home/bar/foo.R"
without hardcoding any filenames in the source?
You can use a hack provided by Gabor a while back by putting this on top of your file :
If you want to extract the name of the directory, you can do :
From my understanding of things, sourcing a file creates two parent environments. The outer one (two steps up) contains the information about the file from where the code is sourced.