Skip to content
Prev 376911 / 398502 Next

Genuine relative paths with R

On 10/10/2018 7:11 PM, Olivier GIVAUDAN wrote:
But it does.  I didn't realize that's what you were asking for.  This 
has nothing to do with your subject line.

If you source a file from somewhere, then each of the functions it 
creates is marked with its source location.  So you can put this in a file:

foo <- function () 1

filename <- normalizePath(getSrcFilename(foo, full.names=TRUE))

(You need the normalizePath() call because all that will be saved is the 
path that was used.  If it was a relative path, that's what you get 
before you normalize it.  You don't really need the foo function; you 
could put an anonymous function into the getSrcFilename call.  It's just 
usually easier to include a function that already exists.)

When you source() that file, filename will get the name of the file it 
came from.

This is a lot like __FILE__ in C.  One difference is that it is usually 
turned off when the function is being loaded into a package, but you can 
optionally turn it on.

You can also find out what line foo starts on, using

fooline <- getSrcLocation(foo)

This is a lot like __LINE__ in C.

Duncan Murdoch

Thread (35 messages)

Jeff Newmiller Genuine relative paths with R Oct 7 Olivier GIVAUDAN Genuine relative paths with R Oct 10 Duncan Murdoch Genuine relative paths with R Oct 10 Olivier GIVAUDAN Genuine relative paths with R Oct 10 Duncan Murdoch Genuine relative paths with R Oct 10 Duncan Murdoch Genuine relative paths with R Oct 10 William Dunlap Genuine relative paths with R Oct 10 Olivier GIVAUDAN Genuine relative paths with R Oct 10 Olivier GIVAUDAN Genuine relative paths with R Oct 10 Duncan Murdoch Genuine relative paths with R Oct 10 Olivier GIVAUDAN Genuine relative paths with R Oct 10 Duncan Murdoch Genuine relative paths with R Oct 10 Olivier GIVAUDAN Genuine relative paths with R Oct 10 Olivier GIVAUDAN Genuine relative paths with R Oct 10 Duncan Murdoch Genuine relative paths with R Oct 10 Olivier GIVAUDAN Genuine relative paths with R Oct 10 Duncan Murdoch Genuine relative paths with R Oct 10 Olivier GIVAUDAN Genuine relative paths with R Oct 10 Duncan Murdoch Genuine relative paths with R Oct 10 Olivier GIVAUDAN Genuine relative paths with R Oct 10 Duncan Murdoch Genuine relative paths with R Oct 10 Olivier GIVAUDAN Genuine relative paths with R Oct 10 Peter Claussen Genuine relative paths with R Oct 10 Duncan Murdoch Genuine relative paths with R Oct 10 Olivier GIVAUDAN Genuine relative paths with R Oct 10 Olivier GIVAUDAN Genuine relative paths with R Oct 10 Fritsch, Katharina (NNL) Genuine relative paths with R Oct 11 Duncan Murdoch Genuine relative paths with R Oct 11 Michael Friendly Genuine relative paths with R Oct 11 Olivier GIVAUDAN Genuine relative paths with R Oct 18 Olivier GIVAUDAN Genuine relative paths with R Oct 18 Olivier GIVAUDAN Genuine relative paths with R Oct 18 Olivier GIVAUDAN Genuine relative paths with R Oct 18 Fritsch, Katharina (NNL) Genuine relative paths with R Oct 19 Olivier GIVAUDAN Genuine relative paths with R Oct 19