Tilde “~” expression not functioning in “R”?

别等时光非礼了梦想. 提交于 2019-12-05 20:47:24

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.

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!