[R] Genuine relative paths with R

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Wed Oct 10 22:54:30 CEST 2018

On 10/10/2018 4:42 PM, Olivier GIVAUDAN wrote:
> Why are you not simply double-clicking on 'TestPWD' and choosing to 
> execute the file (don't add anything)?
> Are you executing the file from a terminal?

Yes, I was executing the file from my terminal.  Otherwise I really have 
no idea what the "current directory" is in the Finder.   (I'm on a Mac. 
I just tried the click method; it printed my home directory, not the 
directory of the script.)

I don't know the name of your visual front end, but you are displaying 
the working directory that it sets when you click on TestPWD.  That will 
be different from the working directory that your user sees in the Terminal.

You can see what I saw if you run TestPWD from the Terminal.  It will 
print the current working directory, not the one where TestPWD happens 
to live.

If you want to do the same sort of thing in R, you could set up a script 
that calls R, and execute that in the way you executed TestPWD.  But in 
another message you said you aren't allowed to do that, so I think your 
best solution is the one offered by Bill Dunlap:  organize your files as 
an R package.  If you name your package "Olivier", then you can find all 
the files in it under the directory returned by

   system.file(".", package = "Olivier")

The package system is designed for R code, but you can put arbitrary 
files into a package:  just store them under the "inst" directory in 
your source.  When the package is installed, those files will be moved 
up one level, i.e.


will become

   system.file("foo", package = "Olivier")

Duncan Murdoch

More information about the R-help mailing list