54

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.

5
  • 8
    try Sys.getenv('R_HOME') or R.home() (faster) Commented Nov 19, 2015 at 7:59
  • you could also go to Tools and Global Options, it will be the first line in General Commented Nov 19, 2015 at 8:00
  • For example in R/RStudio on Linux, system("type R"). On my Linux, it says R is /usr/bin/R. Commented Nov 19, 2015 at 8:04
  • @JorisMeys That's not the OP's question. Commented Nov 19, 2015 at 9: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. Commented Nov 20, 2015 at 1:28

2 Answers 2

41

(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.

Sign up to request clarification or add additional context in comments.

22 Comments

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.
@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).
I just say that my RStudio uses /usr/bin/R, while file.path(R.home("bin"), "R") points to /usr/lib/R/bin/R.
@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.)
@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.
|
3

On macOS and linux @JoshOBrien's solution works great.

However on windows, R sometimes does an irritating thing of returning a windows 'short path' (a legacy convention from DOS days).

For example:

R.home()
# "C:/PROGRA~1/R/R-43~1.1"    # <-- bits of the path is missing

Solution

normalizePath(R.home())
"C:\\Program Files\\R\\R-4.3.1"

# or if it swapped out \ for //, try this instead
gsub("\\\\", "/", normalizePath(R.home()))
"C:/Program Files/R/R-4.3.1"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.