Skip to content
Prev 376915 / 398502 Next

Genuine relative paths with R

I think Gabor (at least) already suggested this solution. But the problem is: how do you source this file containing this 'foo' function without writing its absolute path?

It's a kind of initialisation issue.
It's just a trick to work with relative paths without having to write any hardcoded (by definition) absolute path beforehand.
________________________________
De : Duncan Murdoch <murdoch.duncan at gmail.com>
Envoy? : mercredi 10 octobre 2018 23:51
? : Olivier GIVAUDAN; Jeff Newmiller
Cc : r-help at r-project.org
Objet : Re: [R] 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