Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set PATH on Windows through R "shell" command

I wish to add git to my PATH in Windows 7, through the "shell" command in R.

shell('set PATH=%PATH%;"C:\\Program%20Files%20(x86)\\Git\\bin"', intern = TRUE)
shell("echo %PATH% ", intern= TRUE)

But I do not see that path added.

If I run the above code in cmd.exe, it does add it to PATH.

Any idea what is the issue?

UPDATE: I ended up manually running the following through cmd.exe (which I made sure to run as admin)

setx PATH "C:\\Program Files (x86)\\Git\\bin"

Which worked. But I wish I could have done so through R. (maybe create a temp file and run it?) I then had to restart some programs to make sure they had been affected.

like image 643
Tal Galili Avatar asked Jul 08 '14 02:07

Tal Galili


People also ask

How do I set path in Windows bash?

You need to change this Windows CMD to Git Bash. Go to File > Preferences > Settings and type shell in search settings. After that, navigate to Terminal > Integrated > Shell:Windows and update the path with Git Bash executable: C:\Program Files\Git\bin\bash.exe and save.


2 Answers

If you want to permanantly update your path, then you pretty much had the answer:

shell('setx PATH "C:\\Program Files (x86)\\Git\\bin"')

R only notes a copy of the Windows environment variables when it starts up though, so strsplit(Sys.getenv("PATH"), ";") won't be different until you restart R.

Also, this won't run as with admin permissions (unless you set R as an administrator?) so it will add the path to the user path variable not the system one.


If you want R to see a different path in the current session, just use Sys.setenv.

Sys.setenv(
  PATH = paste(
    Sys.getenv("PATH"), 
    "C:\\Program Files (x86)\\Git\\bin", 
    sep = ";"
  )
)

This won't make permanant changes to the path. Only R can see this change, and only until you close it.

like image 185
Richie Cotton Avatar answered Oct 21 '22 05:10

Richie Cotton


When you run shell, a new process is created. In Windows, this will run CMD.EXE and pass the arguments given. Then this process exits.

When you modify the environment variable, you are modifying in a subprocess of R and not in the R process itself. When the subprocess dies, so does its environment.

You should set the path appropriately before you start R instead.

like image 21
Matthew Lundberg Avatar answered Oct 21 '22 03:10

Matthew Lundberg