Felix Benning
Felix Benning

Reputation: 1192

Trying to add R.exe to the PATH

So I tried adding R to the path on windows 10 (that is supposedly easy).

System Properties -> Environment variables -> Edit -> new: copy and paste: "C:\Program Files\R\R-3.5.0\bin\x64"

Now the thing is, Powershell just refuses to start the R environment when I type in R. R.exe works apparently. Rgui works as well. Is R a reserved letter in powershell or something? It also seems to repeat the previous command sometimes but that doesn't really seem completely consistent either. (I put this entry on top of the list of the path and restarted the pc already)

Upvotes: 6

Views: 10970

Answers (3)

Kourosh Hassani
Kourosh Hassani

Reputation: 26

Run the following code in your console to install the R package. This code will automatically add R to your os PATH.

sudo apt-get install littler

Upvotes: -4

mklement0
mklement0

Reputation: 437953

To generalize Guenther Schmitz' helpful answer:

PowerShell has several types of commands, whose precedence is:

  1. Alias
  2. Function
  3. Cmdlet
  4. External application

Note that name resolution is always case-insensitive, so that both r and R refer to the same command.

That is, before R resolves to R.exe, it is not only an r alias that could get in the way, but potentially also a function or a cmdlet (though the latter case is hypothetical, because well-behaved cmdlets follow a <Verb>-<Noun> naming pattern).

Note that built-in aliases shadowing external programs, especially standard ones, is problematic, and in the context of PowerShell Core a discussion is underway about whether to remove all built-in aliases and make them opt-in only - see this GitHub issue.

To see what a given name resolves to, use the Get-Command cmdlet:

# See what R resolves to
Get-Command R

# See ALL commands that R *can* resolve to, with the EFFECTIVE one listed first:
Get-Command -All R

Choices for unambiguously targeting R.exe:

  • (As you already know) If its folder is in one of the folders listed in environment variable $env:PATH, append .exe - i.e., use the filename extension explicitly:
    R.exe ...

  • Use R.exe's full path (note the need for & for invocation, because the path needs quoting):
    & "C:\Program Files\R\R-3.5.0\bin\x64\R.exe" ...

  • (For the sake of completeness; this is the cumbersome equivalent of using just R.exe): Use Get-Command -Type Application to locate the executable:
    & (Get-Command -Type Application R) ...

Alternatively, as stated in Guenther's answer, you could add Remove-Alias r to your PowerShell $PROFILE file in order to remove the built-in alias on session startup, which then allows you to just use r to start R.exe.

Upvotes: 3

Guenther Schmitz
Guenther Schmitz

Reputation: 1999

when entering get-alias r I got the following result, so yes "r" is already taken ...

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           r -> Invoke-History

PS: you could remove that alias with remove-item alias:\r from your current powershell session and test if "r" then starts "R.exe". if that works for you, you could edit your profile to remove the alias "r -> Invoke-History" from every new session.

Upvotes: 7

Related Questions