[R] Problem with path.expand("~")

Duncan Murdoch murdoch.duncan at gmail.com
Tue Aug 11 17:03:16 CEST 2015


On 11/08/2015 10:51 AM, Sarah Goslee wrote:
> Hm.
> 
> On my linux system:
> 
>> path.expand("~")
> [1] "/home/sarahg"
> 
> The help file says:
>      Expand a path name, for example by replacing a leading tilde by
>      the user's home directory (if defined on that platform).
> 
> Does Windows 7 define ~?
> 
> Just because RStudio defines it for you, doesn't mean that R shares that
> same setup; if R is checking with the operating system, then it depends on
> the Windows setup. You could, I assume, define ~ in Windows yourself, or
> write your own path.expand() to do so. I have no idea which option the
> RStudio designers took.
> 
> Could you use getwd() to get the output you're looking for? I would expect
> ~ to properly be "C:/Users/thierry_onkelinx" and getwd() to be what you've
> set Start in as, suggesting that not only did RStudio define ~ for Windows,
> they did it in a non-standard way. (Unless your home directory on Windows
> should be Documents?)

The tilde meaning "home" is not a concept in Windows, but R fakes it.
This is described in ?path.expand and the R for Windows FAQ.  Thierry
should set the R_USER environment variable to whatever home dir he
wants.  RStudio is probably doing that for him.

Duncan Murdoch

> 
> Sarah
> 
> On Tue, Aug 11, 2015 at 10:41 AM, Thierry Onkelinx <thierry.onkelinx at inbo.be
>> wrote:
> 
>> Dear all,
>>
>> I'm puzzled by the behaviour of path.expand("~")
>>
>> In the RStudio IDE the output is
>>> path.expand("~")
>> [1] "C:/Users/thierry_onkelinx/Documents"
>>
>> In the R GUI the output is
>>> path.expand("~")
>> [1] "~"
>>
>> But I'm expecting the same result as in the RStudio IDE. The "Start in"
>> parameter of shortcut to the R GUI has the value
>> "C:\Users\thierry_onkelinx\Documents"
>>
>> The problem is that I use normalizePath("~/analysis"). This
>> yield "C:\\Users\\thierry_onkelinx\\Documents\\analysis" in RStudio
>>
>> It throws an error in the R GUI
>> "C:\\Users\\thierry_onkelinx\\Documents\\~\\analysis"
>> Warning message:
>> In normalizePath(path.expand(path), winslash, mustWork) :
>>   path[1]="~/analysis": Het systeem kan het opgegeven pad niet vinden
>>
>> # sessionInfo() in RStudio
>> R version 3.2.1 (2015-06-18)
>> Platform: x86_64-w64-mingw32/x64 (64-bit)
>> Running under: Windows 7 x64 (build 7601) Service Pack 1
>>
>> locale:
>> [1] LC_COLLATE=Dutch_Belgium.1252  LC_CTYPE=Dutch_Belgium.1252
>>  LC_MONETARY=Dutch_Belgium.1252
>> [4] LC_NUMERIC=C                   LC_TIME=Dutch_Belgium.1252
>>
>> attached base packages:
>> [1] stats     graphics  grDevices utils     datasets  methods   base
>>
>> loaded via a namespace (and not attached):
>> [1] tools_3.2.1    fortunes_1.5-2
>>
>> # sessionInfo() from R GUI
>> R version 3.2.1 (2015-06-18)
>> Platform: x86_64-w64-mingw32/x64 (64-bit)
>> Running under: Windows 7 x64 (build 7601) Service Pack 1
>>
>> locale:
>> [1] LC_COLLATE=Dutch_Belgium.1252  LC_CTYPE=Dutch_Belgium.1252
>> [3] LC_MONETARY=Dutch_Belgium.1252 LC_NUMERIC=C
>> [5] LC_TIME=Dutch_Belgium.1252
>>
>> attached base packages:
>> [1] stats     graphics  grDevices utils     datasets  methods   base
>>
>> loaded via a namespace (and not attached):
>> [1] tools_3.2.1    fortunes_1.5-2
>>
>>
>> Best regards,
>>



More information about the R-help mailing list