The R Project SVN R

Rev

Rev 74435 | Rev 85606 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
42333 ripley 1
% File src/library/base/man/system.Rd
68948 ripley 2
% Part of the R package, https://www.R-project.org
74434 kalibera 3
% Copyright 1995-2018 R Core Team
42333 ripley 4
% Distributed under GPL 2 or later
5
 
52814 ripley 6
\name{system2}
56186 murdoch 7
\alias{system2}
15874 ripley 8
\title{Invoke a System Command}
9
\description{
53516 ripley 10
  \code{system2} invokes the OS command specified by \code{command}.
15874 ripley 11
}
12
\usage{
52814 ripley 13
system2(command, args = character(),
52854 ripley 14
        stdout = "", stderr = "", stdin = "", input = NULL,
66341 ripley 15
        env = character(), wait = TRUE,
72823 kalibera 16
        minimized = FALSE, invisible = TRUE, timeout = 0)
15874 ripley 17
}
18
\arguments{
52813 ripley 19
  \item{command}{the system command to be invoked, as a character string.}
52814 ripley 20
  \item{args}{a character vector of arguments to \command{command}.}
21
  \item{stdout, stderr}{where output to \file{stdout} or
22
    \file{stderr} should be sent.  Possible values are \code{""}, to the \R
23
    console (the default), \code{NULL} or \code{FALSE} (discard output),
24
    \code{TRUE} (capture the output in a character vector) or a
52854 ripley 25
    character string naming a file.}
26
  \item{stdin}{should input be diverted?  \code{""} means the default,
27
    alternatively a character string naming a file.  Ignored
28
    if \code{input} is supplied.}
40472 ripley 29
  \item{input}{if a character vector is supplied, this is copied one
30
    string per line to a temporary file, and the standard input of
31
    \code{command} is redirected to the file.}
52848 ripley 32
  \item{env}{character vector of name=value strings to set environment
33
    variables.}
52854 ripley 34
  \item{wait}{a logical (not \code{NA}) indicating whether the \R
35
    interpreter should wait for the command to finish, or run it
36
    asynchronously.  This will be ignored (and the interpreter will
75824 kalibera 37
    always wait) if \code{stdout = TRUE} or \code{stderr = TRUE}. When
38
    running the command asynchronously, no output will be displayed on
39
    the \code{Rgui} console in Windows (it will be dropped, instead).}
73040 ripley 40
  \item{timeout}{timeout in seconds, ignored if 0.  This is a limit for the
73045 ripley 41
    elapsed time running \code{command} in a separate process.   Fractions
42
    of seconds are ignored.}
40532 ripley 43
#ifdef unix
53346 ripley 44
  \item{minimized, invisible}{arguments that are accepted on Windows but
45
    ignored on this platform, with a warning.}
40532 ripley 46
#endif
47
#ifdef windows
51190 ripley 48
  \item{minimized}{logical (not \code{NA}), indicates whether the
52801 ripley 49
    command window should be displayed initially as a minimized window.}
51190 ripley 50
  \item{invisible}{logical (not \code{NA}), indicates whether the
51
    command window should be visible on the screen.}
40532 ripley 52
#endif
15874 ripley 53
}
40532 ripley 54
\details{
52814 ripley 55
  Unlike \code{\link{system}}, \code{command} is always quoted by
53611 ripley 56
  \code{\link{shQuote}}, so it must be a single command without arguments.
61433 ripley 57
 
52814 ripley 58
  For details of how \code{command} is found see \code{\link{system}}.
15874 ripley 59
 
66341 ripley 60
  On Windows, \code{env} is only supported for commands such as
61
  \command{R} and \command{make} which accept environment variables on
53639 ripley 62
  their command line.
61433 ripley 63
 
66341 ripley 64
  Some Unix commands (such as some implementations of \code{ls}) change
65
  their output if they consider it to be piped or redirected:
66
  \code{stdout = TRUE} uses a pipe whereas \code{stdout =
67
  "some_file_name"} uses redirection.
53576 ripley 68
 
52814 ripley 69
  Because of the way it is implemented, on a Unix-alike \code{stderr =
53346 ripley 70
    TRUE} implies \code{stdout = TRUE}: a warning is given if this is
71
  not what was specified.
72823 kalibera 72
 
73
  When \code{timeout} is non-zero, the command is terminated after the given
74
  number of seconds.  The termination works for typical commands, but is not
75
  guaranteed: it is possible to write a program that would keep running
72855 kalibera 76
  after the time is out.  Timeouts can only be set with \code{wait = TRUE}.
77
 
78
#ifdef unix
79
  Timeouts cannot be used with interactive commands: the command is run with
80
  standard input redirected from \code{/dev/null} and it must not modify
81
  terminal settings.  As long as tty \code{tostop} option is disabled, which
82
  it usually is by default, the executed command may write to standard
83
  output and standard error.
72823 kalibera 84
#endif
40532 ripley 85
}
53346 ripley 86
%% We use popen, and that pipes stdout only
15874 ripley 87
 
61433 ripley 88
\value{
52814 ripley 89
  If \code{stdout = TRUE} or \code{stderr = TRUE}, a character vector
90
  giving the output of the command, one line per character string.
91
  (Output lines of more than 8095 bytes will be split.)  If the command
53349 ripley 92
  could not be run an \R error is generated.  If \code{command} runs but
57469 ripley 93
  gives a non-zero exit status this will be reported with a warning and
94
  in the attribute \code{"status"} of the result: an attribute
66341 ripley 95
  \code{"errmsg"} may also be available.
40532 ripley 96
 
73621 ripley 97
  In other cases, the return value is an error code (\code{0} for
98
  success), given the \link{invisible} attribute (so needs to be printed
40532 ripley 99
  explicitly).  If the command could not be run for any reason, the
73621 ripley 100
  value is \code{127} and a warning is issued (as from \R 3.5.0).
101
  Otherwise if \code{wait = TRUE} the value is the exit status returned
102
  by the command, and if \code{wait = FALSE} it is \code{0} (the
103
  conventional success value).
72855 kalibera 104
 
73621 ripley 105
  If the command times out, a warning is issued and the exit status is
72855 kalibera 106
  \code{124}.
73621 ripley 107
 
40532 ripley 108
#ifdef windows
52801 ripley 109
  Some Windows commands return out-of-range status values
66444 hornik 110
  (e.g., \code{-1}) and so only the bottom 16 bits of the value are used.
40532 ripley 111
#endif
112
}
113
 
52801 ripley 114
\note{
53346 ripley 115
  \code{system2} is a more portable and flexible interface than
71883 ripley 116
  \code{\link{system}}.  It allows redirection of output without needing
117
  to invoke a shell on Windows, a portable way to set environment
118
  variables for the execution of \code{command}, and finer control over
119
  the redirection of \code{stdout} and \code{stderr}.  Conversely,
120
  \code{system} (and \code{shell} on Windows) allows the invocation of
121
  arbitrary command lines.
52838 ripley 122
 
52859 ripley 123
  There is no guarantee that if \code{stdout} and \code{stderr} are both
124
  \code{TRUE} or the same file that the two streams will be interleaved
125
  in order.  This depends on both the buffering used by the command and
126
  the OS.
52801 ripley 127
}
128
 
52814 ripley 129
\seealso{
130
  \code{\link{system}}.
52801 ripley 131
#ifdef windows
61433 ripley 132
 
53602 ripley 133
  \code{\link{shell}} and \code{\link{shell.exec}}.
40532 ripley 134
#endif
38819 ripley 135
}
15874 ripley 136
\keyword{interface}
137
\keyword{file}
138
\keyword{utilities}