Invoke a System Command
system2 invokes the OS command specified by
system2(command, args = character(), stdout = "", stderr = "", stdin = "", input = NULL, env = character(), wait = TRUE, minimized = FALSE, invisible = TRUE, timeout = 0)
the system command to be invoked, as a character string.
a character vector of arguments to
- stdout, stderr
where output to
stderrshould be sent. Possible values are
"", to the R console (the default),
TRUE(capture the output in a character vector) or a character string naming a file.
should input be diverted?
""means the default, alternatively a character string naming a file. Ignored if
if a character vector is supplied, this is copied one string per line to a temporary file, and the standard input of
commandis redirected to the file.
character vector of name=value strings to set environment variables.
a logical (not
NA) indicating whether the R interpreter should wait for the command to finish, or run it asynchronously. This will be ignored (and the interpreter will always wait) if
stdout = TRUEor
stderr = TRUE. When running the command asynchronously, no output will be displayed on the
Rguiconsole in Windows (it will be dropped, instead).
timeout in seconds, ignored if 0. This is a limit for the elapsed time running
commandin a separate process. Fractions of seconds are ignored.
- minimized, invisible
arguments that are accepted on Windows but ignored on this platform, with a warning.
NA), indicates whether the command window should be displayed initially as a minimized window.
NA), indicates whether the command window should be visible on the screen.
For details of how
command is found see
env is only supported for commands such as
make which accept environment variables on
their command line.
Some Unix commands (such as some implementations of
their output if they consider it to be piped or redirected:
stdout = TRUE uses a pipe whereas
"some_file_name" uses redirection.
Because of the way it is implemented, on a Unix-alike
stdout = TRUE: a warning is given if this is
not what was specified.
timeout is non-zero, the command is terminated after the given
number of seconds. The termination works for typical commands, but is not
guaranteed: it is possible to write a program that would keep running
after the time is out. Timeouts can only be set with
wait = TRUE.
Timeouts cannot be used with interactive commands: the command is run with
standard input redirected from
/dev/null and it must not modify
terminal settings. As long as tty
tostop option is disabled, which
it usually is by default, the executed command may write to standard
output and standard error.
stdout = TRUE or
stderr = TRUE, a character vector
giving the output of the command, one line per character string.
(Output lines of more than 8095 bytes will be split.) If the command
could not be run an R error is generated. If
command runs but
gives a non-zero exit status this will be reported with a warning and
in the attribute
"status" of the result: an attribute
"errmsg" may also be available.
In other cases, the return value is an error code (
success), given the invisible attribute (so needs to be printed
explicitly). If the command could not be run for any reason, the
127 and a warning is issued (as from R 3.5.0).
wait = TRUE the value is the exit status returned
by the command, and if
wait = FALSE it is
conventional success value).
If the command times out, a warning is issued and the exit status is
Some Windows commands return out-of-range status values
-1) and so only the bottom 16 bits of the value are used.
system2 is a more portable and flexible interface than
system. It allows redirection of output without needing
to invoke a shell on Windows, a portable way to set environment
variables for the execution of
command, and finer control over
the redirection of
shell on Windows) allows the invocation of
arbitrary command lines.
There is no guarantee that if
stderr are both
TRUE or the same file that the two streams will be interleaved
in order. This depends on both the buffering used by the command and