On R for Windows, there are two functions to invoke system commands. On the one hand it is possible to use system() (or the newer system2() function) to execute system commands. On the other hand, it is possible to use shell(). In my opinion they both should do the same. What would be the difference between them?
From reading the documentation, shell is described as a more user-friendly wrapper around system. I can see that both have slightly different arguments, but I don't really see the point of calling shell more user-friendly. Except for this, I cannot see any other differences and even executing some basic code gives the same results (except for the quotes).
> system('ECHO "test"')
test
> shell('ECHO "test"')
"test"
What would be the argument to use one rather than the other?
The system( ) function works differently from the exec*( ) functions; instead of replacing the currently executing program, it creates a new process with fork( ) . The new process executes the shell with execve( ) while the original process waits for the new process to terminate.
system() will execute the supplied command in a child process that it spawns. exec() will replace the current process with the invocation of the new executable that you specify. If you want to spawn a child process using exec , you'll have to fork() your process beforehand.
If your system commands are accepted by CMD.EXE
(default on Windows), there isn't much difference. However if you want to use different shells like sh
to execute non-CMD.exe scripts, it can save you having to prefix every system command. It will also help with related issues such as having to switch out every /
for \
.
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