Tilde "~" expression not functioning in "R"? - r

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?

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

Related

RStudio and R have different `~` (Home) directories

I have just updated my R version from 4.1.1 to 4.2.2. My problem is that RStudio (2022.07.2) is not appearing to process the .Renviron file correctly.
In other words, I get the following in the console:
> path.expand("~")
[1] "C:/Users/username/Documents"
> Sys.getenv("home")
[1] "C:/Users/username/OneDrive - Co Inc/Documents"
I suspect that a part of the problem was that version 4.1.1 was moved to a OneDrive folder by my company's IT department whereas 4.2.2 is not on a OneDrive folder.
The problem with RStudio not recognising ~ is that I refer to ~ numerous times in my existing code to point to the OneDrive directory and its numerous subfolders.
One thing that is odd is that I get the following when I run R directly.
> path.expand("~")
[1] "C:/Users/username/OneDrive - Co Inc/Documents"
I have searched my c: drive for .Renviron files and can only find 2:
Located in C:/Users/username/OneDrive - Co Inc, which I presume was created for 4.1.1.
Located in C:/Users/username, which I presume was created for 4.2.2.
Both of these version include the following lines:
RUSER=C:/Users/username/OneDrive - Co Inc/Documents
HOME=C:/Users/username/OneDrive - Co Inc/Documents
I've also checked both versions' copy of the .Rprofile files. Both include these lines:
Sys.setenv(HOME="C:/Users/username/OneDrive - Co Inc/Documents")
Sys.setenv(R_USER="C:/Users/username/OneDrive - Co Inc/Documents")
A workaround is to start every R script with the Sys.setenv(HOME="C:/Users/username/OneDrive - Co Inc/Documents"), though that would seem like it defeats the purpose of .Renviron.
I've spent literally hours googling to find a solution, which I suspect is related to migrating from a version inside OneDrive to one outside of OneDrive.
Pages that I've studied include (but not limited to):
https://support.posit.co/hc/en-us/articles/360047157094-Managing-R-with-Rprofile-Renviron-Rprofile-site-Renviron-site-rsession-conf-and-repos-conf
Cannot locate .Rprofile file
Tilde expansion in RStudio on Windows
RStudio home directory doesn't exist
Difference between tilde expansion in RStudio and R
Tilde "~" expression not functioning in "R"?
The documents say that on Windows, path.expand("~") will give the contents of the R_USER environment variable. The Windows FAQ also mentions that R looks at the HOME environment variable if R_USER is not set. If you are getting different results in RStudio vs. R, it must be because of some difference in those environment variables. The way to see their settings is to start a new session and run
Sys.getenv("HOME")
Sys.getenv("R_USER")
If you don't see any differences in the results between the two systems, then something very strange is going on, but I think you will. So the next step is to figure out where the difference came from.
You mention two .Renviron files containing settings for RUSER and HOME. If that's not a typo on the first one, then those files aren't setting R_USER (it's not the same as RUSER) and HOME looks the same,
so you need to look somewhere else for where R_USER is coming from.
There are several ways to set environment variables in Windows. I've never used Windows 11 so it's possible things have changed, but the basic methods in previous versions are these:
Set system-wide or user-specific environment variables in the "Settings".
Set session-specific environment variables in a command shell.
(Maybe, I forget...) Set shortcut-specific environment variables.
In R, there are also several different places to look for .Renviron or .Rprofile or one of the system versions of those files: see the ?Startup help page for the gory details. Somewhere in there you should find the bad setting that RStudio is using.

How stop RStudio from creating empty "R" folder within "/home" directory at every startup

After having set the path for the default working directory as well as my first (and only) project within RStudio options I wonder why RStudio keeps creating an empty folder named "R" within my "/home" directory every time it is started.
Is there any file I could delete/edit (eventually create) to stop this annoying behaviour and if so, where is it located ?
System: Linux Mint v. 19.3
Software: RStudio v. 1.3.959 / R version 3.4.4
Thanks in advance for any hints.
Yes, you can prevent the creation of the R directory — R is configurable via a set of environment variables.
However, setting these correctly isn’t trivial. The first issue is that many R packages are sensitive to the R version they’re installed with. If you upgrade R and try to load the existing package, it may break. Therefore, the R package library path should be specific to the R version.
On clusters, an additional issue is that the same library path might be read by various cluster nodes that run on different architectures; this is rare, but it happens. In such cases, compiled R packages might need to be different depending on the architecture.
Consequently, in general the R library path needs to be specific both to the R version and the system architecture.
Next, even if you configure an alternative path R will silently ignore it if it doesn’t exist. So be sure to manually create the directory that you’ve configured.
Lastly, where to put this configuration? One option would be to put it into the user environment file, the path of which can be specified with the environment variable R_ENVIRON_USER — it defaults to $HOME/.Renviron. This isn’t ideal though, because it means the user can’t temporarily override this setting when calling R: variables in this file override the calling environment.
Instead, I recommend setting this in the user profile (e.g. $HOME/.profile). However, when you use a desktop launcher to launch your RStudio, this file won’t be read, so be sure to edit your *.desktop file accordingly.1
So in sum, add the following to your $HOME/.profile:
export R_LIBS_USER=${XDG_DATA_HOME:-$HOME/.local/share}/R/%p-library/%v
And make sure this directory exists: re-source ~/.profile (launching a new shell inside the current one is not enough), and execute
mkdir -p "$(Rscript -e 'cat(Sys.getenv("R_LIBS_USER"))')"
The above is using the XDG base dir specification, which is the de-facto standard on Linux systems.2 The path is using the placeholders %p and %v. R will fill these in with the system platform and the R version (in the form major.minor), respectively.
If you want to use a custom R configuration file (“user profile”) and/or R environment file, I suggest setting their location in the same way, by configuring R_PROFILE_USER and R_ENVIRON_USER (since their default location, once again, is in the user home directory):
export R_PROFILE_USER=${XDG_CONFIG_HOME:-$HOME/.config}/R/rprofile
export R_ENVIRON_USER=${XDG_CONFIG_HOME:-$HOME/.config}/R/renviron
1 I don’t have a Linux desktop system but I believe that editing the Env entry to the following should do it:
Exec=env R_LIBS_USER=${XDG_DATA_HOME:-$HOME/.local/share}/R/%p-library/%v /path/to/rstudio
2 Other systems require different handling. On macOS, the canonical setting for the library location would be $HOME/Library/Application Support/R/library/%v. However, setting environment variables on macOS for GUI applications is frustratingly complicated.
On Windows, the canonical location is %LOCALAPPDATA%/R/library/%v. To set this variable, use [Environment]::SetEnvironmentVariable in PowerShell or, when using cmd.exe, use setx.

How/When is the HOME environment variable set within R

Asked on superuser and got crickets, so trying here. This one seems to straddle the border of SU/SO.
In troubleshooting some kind of R configuration issue that was causing a pandoc conversion failure when trying to Knit a .Rmd on a colleague's Windows 10 machine, I noticed that the first path in .libPaths() was pointing to a path on a network directory rather than the c:/Users/[username]/R/win-library/... directory.
Running Sys.getenv() in R showed that HOMEDRIVE and HOMEPATH were (as expected) c: and \Users\[username], yet there was a HOME environment variable listed that was pointing to the network path that we'd found in .libPaths()
Running SET in a cmd shell did not list this HOME environment variable at all, so it seems to be something that R found somewhere else...
Where does R get this HOME environment variable?
FWIW: I fixed the config issue by setting a Windows User Environment variable HOME=%HOMEDRIVE%%HOMEPATH%; R then set all the other environment variables appropriately from there.
R startup is somewhat complicated, but it is pretty well documented. The usual starting place is help("Startup"). The answer to your question is not documented there, but you will find this clue in the See also section:
For the definition of the ‘home’ directory on Windows see the ‘rw-FAQ’
Q2.14. It can be found from a running R by Sys.getenv("R_USER")
and indeed the cited FAQ at https://cran.r-project.org/bin/windows/base/rw-FAQ.html#What-are-HOME-and-working-directories_003f gives us the answer:
The home directory is set as follows: If environment variable R_USER
is set, its value is used. Otherwise if environment variable HOME is
set, its value is used. After those two user-controllable settings, R
tries to find system-defined home directories. It first tries to use
the Windows "personal" directory (typically
C:\Users\username\Documents). If that fails, if both environment
variables HOMEDRIVE and HOMEPATH are set (and they normally are), the
value is ${HOMEDRIVE}${HOMEPATH}. If all of these fail, the current
working directory is used.
You might want to take a look at the here package and, relating to knitr, the ezknitr package. Also, in RStudio, you can specify in the knit menu if the knitting will happen in the current dir, working dir or project dir, as shown here.
Update: let me reiterate the message here since it got downvoted: in a typical usage scenario you don't have to modify the HOME environment variable. Using a proper workflow (e.g. RStudio projects or the here package) is a more robust and portable solution.

Tilde expansion in RStudio on Windows

When working on a unix system, the ~ expands my directory to my unix home. When on my windows computer, I would like ~ to expand to the drive that is mapped and points to the Unix home. I am using RStudio for coding on the windows computer and it expands the ~ to something that isn't helpful and I am having trouble changing it. I have played with the environment variables and PATH but cannot get it to point to what I want. Any ideas?
UPDATE:
Per Josh's answer. Changing the R_USER environment variable in windows, prior to starting RStudio yields on startup:
Error: invalid version specification ‘NA’
In addition: Warning message:
In utils:::packageDescription(packageName, fields = "Version") :
no package 'rstudio' was found
Changing it manually every time after RStudio startup is possible using this answer but I would like to avoid doing that.
To change the value of ~ from its default, you need to set R_USER before your first call to path.expand() et al. (This is documented in ?path.expand.)
Try this:
## R
Sys.getenv("R_USER")
# [1] "C:\\Users\\Josh"
Sys.setenv(R_USER="C://")
path.expand("~")
# [1] "C://"
To set the starting value of "R_USER" for all of your R/Rstudio sessions, just add a line like the following to your ~/.Renviron or $R_HOME/etc/Renviron.site or wherever you prefer. (As always, see ?Startup for the full set of options.):
R_USER = "C:/"
What is your .libPaths() after starting RStudio? I note that if I start RStudio on Windows without setting R_USER, I get:
> .libPaths()
[1] "C:/Users/Kevin/Documents/R/win-library/3.1"
[2] "C:/Program Files/R/R-3.1.0/library
The first library path is automatically generated by RStudio, and it installs rstudio and manipulate packages there automatically on startup (because it may not have permissions to write to the default, 'system' library).
However, if R_USER is set, then that library path is not set, and this appears to cause problems with the installation of these packages.
I believe you should be able to work around this by also setting the R_LIBS_USER environment variable accordingly -- it should be set to somewhere that RStudio has permissions to write to. See ?libPaths for more information on how that might be appropriately set.
Consider using ${} in Renviron.site to call Windows environment variables. To make R_USER point to each user's document folder, you can put the following in Renviron.site:
R_USER=C:/Users/${USERNAME}/Documents
I have a similar situation and for my needs it was easiest to redefine the HOME-parameter with the line below:
Sys.setenv(HOME="path/to/desired/tilde-folder/")
path.expand("~") # Check that it worked.
Update: Just realised that this solution does not appear to work on my Windows machine, only on a Linux server I was using.

change r-library directory windows 8.1

Hello this is likely to be a silly question, sorry for that.
I want to change the library folder in Windows but I do not understand what is said in the cran website:
You may also want to add command-line arguments at the end of the Target field (after any final double quote, and separated by a space), for example --sdi --max-mem-size=1G. You can also set environment variables at the end of the Target field, for example R_LIBS=p:/myRlib, and if you want to ensure that menus and messages are in (American) English, LANGUAGE=en.
When I set the R_LIBS variable in R with R_LIBS="C:/R/i686-pc-linux-gnu-library/3.0" it doen't give any error but the folder path for the library does not change becuse when I type require(package) it is not found.
I saw this post and it says it can be due to the R.profile. I don't know how to deal with the error, should I just remove R and install it from the beginning?
You should set the environment variable R_LIBS in Windows ( not within R) to something like
R_LIBS=C:/R/i686-pc-linux-gnu-library/3.0
Restart R.
Note to set the environment variable you can use setx command:
Open a cmd console (command line console)
setx R_LIBS %R_LIBS%;C:/R/i686-pc-linux-gnu-library/3.0;

Resources