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
.)
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.
If we want to check the current directory of the R script, we can use getwd( ) function. For getwd( ), no need to pass any parameters. If we run this function we will get the current working directory or current path of the R script. To change the current working directory we need to use a function called setwd( ).
One of the great things about using RStudio Projects is that when you open a project it will automatically set your working directory to the appropriate location. You can check the file path of your working directory by looking at bar at the top of the Console pane.
When editing files in the RStudio editor, any selection (or the current line if nothing is selected) can be sent to the active terminal via Ctrl+Alt+Enter (also Cmd+Alt+Enter on the Mac). If a single-line was sent to the terminal the cursor will advance automatically to the next line, allowing single-stepping.
(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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With