43

Original Question

This seems easy and has likely been asked before, but I could not find it via a search.


I have a few flavors of R installed. I simply want to know, when I run RStudio, which flavor of R is it pointing to. So, I need a command -- within RStudio itself, ideally -- that can tell me the underlying R executable that is being used for this RStudio window that I am currently working with.


To be clear, I do not need / want to know the version of R that I'm using (e.g., R version 3.2.2 (2015-08-14) -- 'Fire Safety'). Instead, I want to know the actual path that RStudio is using to get to R -- looking at it from within RStudio -- so that I know "for reals" which version it's using. (E.g., /usr/local/bin/R.)


Edit & Answer

There are a lot of great discussions here, and some are OS-specific. I have a Mac. In my case, I found that:

> system("type R")
R is /usr/local/bin/R

> R.home()
[1] "/usr/local/Cellar/r/3.2.2_1/R.framework/Resources"

> file.path(R.home("bin"), "R")
[1] "/usr/local/Cellar/r/3.2.2_1/R.framework/Resources/bin/R"

As those of you familiar can see, I am using brew. If I look for /usr/local/bin/R outside of R, I see:

$ ls -l /usr/local/bin/R
lrwxr-xr-x  1 mike  admin  25 Nov 14 17:31 /usr/local/bin/R -> ../Cellar/r/3.2.2_1/bin/R

which eventually resolves (2 symbolic links) to:

/usr/local/Cellar/r/3.2.2_1/R.framework/Resources/bin/R

as the final destination.

So on my system (Mac OS X), file.path(R.home("bin"), "R") was the most accurate.

Mike Williamson
  • 4,915
  • 14
  • 67
  • 104
  • 7
    try `Sys.getenv('R_HOME')` or `R.home()` (faster) – etienne Nov 19 '15 at 07:59
  • you could also go to Tools and Global Options, it will be the first line in General – etienne Nov 19 '15 at 08:00
  • For example in R/RStudio on Linux, `system("type R")`. On my Linux, it says `R is /usr/bin/R`. –  Nov 19 '15 at 08:04
  • @JorisMeys That's not the OP's question. –  Nov 19 '15 at 09:53
  • Side note: On my Linux, `file.path(R.home("bin"), "R")` doesn't return the path to the executable currently used by RStudio. This command `system("type R | xargs readlink")` does. –  Nov 20 '15 at 01:28

1 Answers1

33

(Edited to reflect fact that this is apparently a Windows-specific solution.)

Here on Windows, I'd use the following, for reasons discussed here by Henrik Bengtsson near the start of a long thread on the subject.

file.path(R.home("bin"), "R")

This is better than using file.path(R.home(), "bin", "R") in several settings alluded to in the "Value" section of this snippet from help(R.home):

Details:

The R home directory is the top-level directory of the R installation being run.

[...]

Value:

A character string giving the R home directory or path to a particular component. Normally the components are all subdirectories of the R home directory, but this may not be the case in a Unix-like installation. [...] The return value for "modules" and on Windows "bin" is to a sub-architecture-specific location.

Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
  • But it doesn't give the current path used. On my machine, it gives `/usr/lib/R/bin/R`, which is incorrect. My default R binary is located in `/usr/bin/R`. If I change it for `/usr/bin/R.old`, RStudio won't run. –  Nov 19 '15 at 08:35
  • @Pascal Can you be more specific? Do you mean that it doesn't point to `Rcmd.exe` or `Rgui.exe` or `Rterm.exe`, if those are what were used? If so I agree, but (guessing here) think the OP is just wanting the path along which the version of R that RStudio's using is found. If you're saying that it gives that path incorrectly, though, I'll have to disagree (until you show me an example). – Josh O'Brien Nov 19 '15 at 08:42
  • I just say that my RStudio uses `/usr/bin/R`, while `file.path(R.home("bin"), "R")` points to `/usr/lib/R/bin/R`. –  Nov 19 '15 at 08:43
  • @Pascal It's been a long time since I've used R on *NIX, but is there any chance that `/usr/bin/R` is a script that eventually launches the executable at `/usr/lib/R/bin/R`? (I'd check myself, but don't have access a non-Windows machine right now.) – Josh O'Brien Nov 19 '15 at 08:50
  • Just checked on my Linux. First, `/usr/bin/R` is not a link to `/usr/lib/R/bin/R` (checked with `file /usr/bin/R`). Then, using `diff -y <(xxd /usr/bin/R) <(xxd /usr/lib/R/bin/R) | colordiff` shows no difference between two binaries. –  Nov 19 '15 at 09:02
  • @Pascal Interesting. Thanks for checking. (FWIW, I wasn't expecting it to be a link, but do recall that long ago (5+ years) typing R from the *NIX command line found a shell script `R.sh`, in `/usr/local/bin`, and that script would then invoke the R binary, found elsewhere. Doing `whereis R` (back then) might only find the shell script and not the actual compiled executable.) – Josh O'Brien Nov 19 '15 at 09:18
  • On my Linux, I get `R: /usr/bin/R /usr/lib/R /usr/share/man/man1/R.1` for `whereis R`. –  Nov 19 '15 at 09:19
  • 1
    @Pascal Well, if `/usr/bin/R` not a shell script and if that's what Rstudio's actually launching, I'm pretty much at a loss as to what's going on. It certainly doesn't seem to fit with the documented behavior of `?R.home` (see the `"Details"` section), nor with the general tenor of Simon Urbanek's comments in the thread linked in my answer. Oh well, I'm off for the evening. – Josh O'Brien Nov 19 '15 at 09:28
  • Actually, you should just try R.home(). That's the top-level directory from which R is run. On Linux, the "bin" folder of the R installation doesn't need to be in the same location as the actual R installation. That's why I wonder why @JoshO'Brien uses R.home("bin"). This doesn't seem the information OP is after. – Joris Meys Nov 19 '15 at 09:52
  • @Pascal: what does `Sys.getenv('R_HOME')` and `R.home()` return for you (I'm on Windows so can't test for Linux) – etienne Nov 19 '15 at 10:01
  • @etienne Both gives `/usr/lib/R`. –  Nov 19 '15 at 10:02
  • @Pascal I misunderstood the question apparently, my bad. But on a Linux system you still shouldn't use 'bin' but 'modules'. 'bin' is -to my understanding- Windows specific. What do you get from `file.path(R.home("modules"), "R")` – Joris Meys Nov 19 '15 at 13:46
  • @JorisMeys On a lot of Linux distributions, binaries are located in `/usr/bin` and `/usr/local/bin`. On Windows, it is `Program Files`. –  Nov 19 '15 at 13:50
  • @Pascal I quote from the help file of `?R.home`: The value for "modules" and on Windows "bin" is a sub-architecture-specific location. I'm talking about the settings for `R.home()`, not the directory structure of the OS. So what do you get? – Joris Meys Nov 19 '15 at 13:52
  • @JorisMeys Yes, sorry. Well, I get `"/usr/lib/R/bin/R"`, which is correct. But as I said previously, this executable is not used by my RStudio. –  Nov 19 '15 at 13:55
  • @Pascal What do you get when you directly launch `/usr/lib/R` (outside of RStudio) and do `R.home()` from the launched session? – Josh O'Brien Nov 19 '15 at 16:37
  • @JoshO'Brien I get nothing, as `/usr/lib/R` is a directory. –  Nov 20 '15 at 00:35
  • @Pascal -- My bad, I meant what do you get when you launch `usr/bin/R`? – Josh O'Brien Nov 20 '15 at 00:41
  • @JoshO'Brien It gives `/usr/lib/R`. So, in summary, your answer applied to Windows, while OP's edit applies to OSX. But it doesn't apply to Linux, or at least to my installation. If I create a symbolic link in `ln -s /usr/bin/R ~/bin/R`, then run `~/bin/R`, I get for `system("type R")` `R is /home/xxx/bin/R`, which is incorrect (it shows the symbolic link). `R.home()` still gives `"/usr/lib/R"`. Finally, this command is the correct approach: `system("type R | xargs readlink")`, which gives `/usr/bin/R`. –  Nov 20 '15 at 00:48
  • @Pascal Or perhaps your RStudio actually does use `/usr/lib/R/bin/R`, and is trying to tell you so? (Not trying to be argumentative -- just wondering how you know that RStudio is using `/usr/lib/R` when it tells you otherwise. `system("type R")` after all tells you about the R that's first on your shell's search path, not necessarily the one you're currently running.) – Josh O'Brien Nov 20 '15 at 01:00
  • This is a good answer. Another for the binary executable path, from interactive mode, is: `commandArgs()[1]` – Jack Wasey Jul 16 '20 at 19:29
  • Why is the path truncated ? – Julien Jun 10 '23 at 08:48