5

The tilde expression is not working on my RStudio! I am using a windows 10 OS.

Update : After looking more carefully(thanks to Dason) It seems the "~" is pointing to "user/Documents/" rather then "user/". is there any work around?

Image

Any help?

Heroka
  • 12,359
  • 1
  • 25
  • 38
data9
  • 67
  • 1
  • 7
  • 3
    Are you sure "~" points where you think it points? – Dason Dec 21 '15 at 17:10
  • 5
    Also never post an image when you can post the actual code/results. – Dason Dec 21 '15 at 17:11
  • Ideally it should point to home directory, shouldn't it? Ok Thanks, I will keep that in mind next time. – data9 Dec 21 '15 at 17:11
  • Dear Dason, Yes it's actually pointing to a different folder the "documents" folder. How can I rectify that? – data9 Dec 21 '15 at 17:21
  • You've not shown a single call with `~` working. What does `setwd("~/")` do? Then what does `getwd()` say after that? How do you assume it points to `user/Documents`? – Spacedman Dec 21 '15 at 17:47
  • no one mentioned `path.expand('~')` yet? – rawr Dec 21 '15 at 17:53
  • Hello spacedman, actually I 'setwd("~/")' is working fine and 'getwd()' showing the path of documents folder. That's how I got to know the actual point of '~'. – data9 Dec 22 '15 at 03:45

3 Answers3

4

The home directory in Windows R is set using the environment variable R_USER Set this using Windows (search from the Start Menu for "environment variable"). Whatever you set this to will become what R uses for ~. I have mine set to C:\Users\trehman\R.

Here is how mine looks.

enter image description here

TARehman
  • 6,281
  • 3
  • 31
  • 54
  • 1
    Thanks TARehman, Looks like a promising solution, will let you know after trying it. Thanks again for you time N help. – data9 Dec 22 '15 at 03:55
  • i added [~] to both user and system environment with value [C:/users/user/] but still no change can you guide me with a bit elaboration. – data9 Dec 22 '15 at 17:47
  • @Mrinal9 Does it look like mine? – TARehman Dec 22 '15 at 17:58
  • No actually there is no R_user variable in my Environment variable. I had added [~] as a variable with a value to my home folder. Let me try like yours & see. – data9 Dec 22 '15 at 18:02
  • Yep! Now its working fine. Thanks a ton again for the generous help sir. – data9 Dec 22 '15 at 18:06
2

As you found out yourself, R's ~ points to your documents folder. An easy way to work around this could be to start the paths you want to reference with ~/../. So your setwd call would look like this:

setwd('~/../{your actual path from home}')

The same problem occurs on my Windows PC. I do not have a Linux machine at hand to test whether it is the same there. R gets this variable from the R_USER environment variable. You can override that in your R environment startup file. This issue is not specific to RStudio and also occurs when you start an R session from the command line.

Patrick Kostjens
  • 4,785
  • 6
  • 26
  • 43
0

At startup, R looks for Renviron.site in the R_HOME directory. Open and edit it

file.edit(file.path(Sys.getenv('R_HOME'), 'etc', 'Renviron.site'))

Define R_USER in that file with the path you want '~' to point to:

R_USER = 'C:/Users/USERNAME'

References: R for Enterprise: Understanding R’s Startup