Reputation: 3024
How does R's built-in system()
function know where to look to invoke some arbitrary OS command specified by the command
argument? For example, if I homebrew install some_command_line_program
, how does R's system()
function know where it is located when I type:
cmd <- "some_complicated_code_from_some_command_line_program"
system(cmd, wait=FALSE)
In other words, how is R smart enough to know where to look without any user input? If I compile from source via Github (instead of homebrew install
), would system()
also recognize the command?
Upvotes: 0
Views: 1031
Reputation: 94182
What system
does depends on your OS, you've not told us (although you've given us some clues...).
On unix-alike systems, it executes it as a command in a bash shell, which searches for the first match in the directories on the $PATH environment variable. You can see what that is in R:
> Sys.getenv("PATH")
[1] "/usr/local/heroku/bin:/usr/local/heroku/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/nobackup/rowlings/NEST4B"
In Windows, it does something else.
You can get a full path to whatever it might run with Sys.which
, which uses the systems' which
command on unixes and fakes it on Windows. Read the help for more.
If you compile something from source then it will be found if the file that runs the command (a shell script, an executable, a #!-script in any language) is placed in a folder in your $PATH
. You can create a folder yourself, say /home/user/bin
, put your executables in there, add that to your $PATH
, and (possibly after logging out an in again, or restarting R, or just firing up a new shell...) then R will find it.
Upvotes: 1