[R] Get "home" directory and simple I/O

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Mar 23 15:36:27 CET 2007


But the request was for a *generic* solution.  On Windows there
might not be anything corresponding to a home directory (and the rw-FAQ 
discusses the concept and how R resolves this).

The best answer I know of is path.expand("~").

On Fri, 23 Mar 2007, Henrik Andersson wrote:

> Sys.getenv("HOME") works in Linux at least

But not all Unix shells have it set, and a user can unset or reset it.  To 
be perverse:

gannet% unsetenv HOME
gannet% R --slave
Sys.getenv("HOME")
HOME
   ""
path.expand("~")
[1] "/data/gannet/ripley"
q()

(If you set HOME to something other than your home directory it will be 
honoured.)

>
> - Henrik
>
> Andris Jankevics wrote:
>> If you want get a username of user currently running R on Linux,you can use a
>> system command and read enviroment variables:
>>
>> paste("/home/",system ("whoami",intern=TRUE),sep="")

And my home directory is not '/home/ripley' on any of the Linux boxes I 
use (even though it exists on some of them), e.g. on my compute server

> path.expand("~")
[1] "/data/gannet/ripley"

and on our main cluster

> path.expand("~")
[1] "/home/markov/ripley"


>> Andris Jankevics
>>
>> On Piektdiena, 23. Marts 2007 14:30, Alberto Monteiro wrote:
>>
>>> Is there any generic function that gets the "home" directory? This
>>> should return /home/<user> in Linux and
>>> x:/Documents and Settings/<user> (or whatever) in Windows XP.

[...]

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list