2

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.

dormilon
  • 21
  • 1
  • 2
  • 2
    I just tested on R-3.5.3 on ubuntu-16.04, and it does not automatically create an `~/R/` directory. My guess is that either (1) it's part of RStudio (sorry, I don't have it installed on my ubuntu systems), or (2) there is something in your `~/.Rprofile` that is doing it. (My bet is on #1.) – r2evans Jul 29 '20 at 23:11
  • What does your `.libPaths()` look like? And does it do the same thing when you run R directly rather than trough RStudio? Is there a reason you are using such an old version of R? – MrFlick Jul 29 '20 at 23:27
  • 3
    Is it `R` or `.R`? – itsMeInMiami Jul 29 '20 at 23:31
  • @r2evans In fact, it's RStudio which creates the empty "R" while R-3.4.4 when started from command line does not. But as a total newbie to R I don't feel like managing it through command line is a valid option to me. – dormilon Jul 30 '20 at 10:10
  • @itsMeInMiami Both folders are created (by RStudio). I guess the latter one has to deal with my permission granted to save crash reports. – dormilon Jul 30 '20 at 10:11
  • @MrFlick When starting R alone no extra folder is created, so it's RStudio which is annoying me. I'm simply using the version of R offered within Linux Mint "Synaptic" package management. Since my current aim is to learn using R and perform rather simple things I haven't encountered any caveats caused by "old" 3-4.4 version by now. – dormilon Jul 30 '20 at 10:18
  • 1
    I understand your frustration, and I don't know of a way to prevent RStudio from creating that directory. You might consider this, though: in R, when you install a package that is not included with base R, as a *user*, you do not have permission to install packages into the system library (often `/usr/lib/R` or `/usr/local/lib/R`). The only way to allow you to install packages into R is to have a directory which is writable by you. Ergo the `~/R/version` directory. If you're learning R, I almost guarantee you'll want to install a package or two. – r2evans Jul 30 '20 at 13:37
  • @r2evans But that’s patently not the correct directory location. Don’t clutter the user’s home directory. – Konrad Rudolph Sep 23 '20 at 21:54
  • Perhaps I am misunderstanding your point, @KonradRudolph. `?.libPaths` specifically says *"By default R_LIBS is unset, and R_LIBS_USER is set to directory ‘R/R.version$platform-library/x.y’ of the home directory"*, which to me means that `~/R/...` is the default location. It's personal preference whether this is clutter, but regardless it is the defined location. – r2evans Sep 23 '20 at 22:21
  • @r2evans … and by doing so R is breaking OS convention. Because apps aren’t supposed to write there, precisely to avoid cluttering the user home. These things belong e.g. in `$XDG_DATA_HOME`. That being said, I’m less concerned with R itself, since it’s at least configurable. But RStudio arrogantly flouts this rule and writes not one, but *two* rubbish folders into the user home directory (`~/.r` and `~/.rstudio-desktop`) without any possibility of preventing this. If *only* one application did this it would be a mere aesthetic flaw but unfortunately there are many offenders. – Konrad Rudolph Sep 24 '20 at 07:27
  • I don't necessarily disagree with you, but ... that's been the R way for *years*, so it is "the default behavior". I agree that `XDG_DATA_HOME` makes sense. A [suggestion](https://stat.ethz.ch/pipermail/r-devel/2019-May/077782.html) (May 2019) to the `r-devel` mailing list unfortunately seems to have gone unanswered. Perhaps my [prev comment](https://stackoverflow.com/questions/63163719/how-stop-rstudio-from-creating-empty-r-folder-within-home-directory-at-ever?noredirect=1#comment111712944_63163719) should have said "the default of" instead of suggesting it is ideal. – r2evans Sep 24 '20 at 14:18

1 Answers1

3

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.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 4
    Incidentally, the fact that this setting isn’t the default but requires ~1 A4 page of dense explanation is mildly infuriating. – Konrad Rudolph Sep 24 '20 at 08:32
  • 1
    Indeed, depending on your distribution, the most convenient way of making this work in Rstudio (as opposed to R started from the command line) may be to edit the `.desktop` file. On my system (which is Manjaro), I had the choice between the two miserable options of either moving `/usr/share/applications/rstudio.desktop` to `~/.local/share/applications/` or editing it in-place. Either way, replacing the current `Exec = ...` line with `Exec=env R_LIBS_USER=${XDG_DATA_HOME:-$HOME/.local/share}/R/%p-library/%v /usr/bin/rstudio-bin %F` (and making sure that folder exists like you describe), works. – Sixtyfive Aug 28 '21 at 10:54
  • This may be a slightly amateur question to ask but could I not just simply make /usr/lib/R/library writable using `chmod /usr/lib/R/library +w`? I'm the only user on my laptop – JJGabe Jan 07 '23 at 16:20
  • @JJGabe This is fundamentally a bad idea. First off, it compromises system security: even as the only user, you do *not* want these directories writable to the regular user account; it’s a basic malware barrier. And secondly, it’s just unnecessary. If you are the only user on the system, the default settings of a new R installation already use the correct directories without requiring any manual configuration. – Konrad Rudolph Jan 08 '23 at 10:12