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

青春壹個敷衍的年華 提交于 2019-12-07 18:20:02

问题


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?

Any help?


回答1:


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.




回答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.




回答3:


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



来源:https://stackoverflow.com/questions/34400903/tilde-expression-not-functioning-in-r

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