The R Project SVN R

Rev

Rev 59039 | Rev 66444 | 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
2
% Part of the R package, http://www.R-project.org
59039 ripley 3
% Copyright 1995-2011 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,
52848 ripley 15
        env = character(),
52854 ripley 16
        wait = TRUE, minimized = FALSE, invisible = TRUE)
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
37
    always wait) if \code{stdout = TRUE}.}
40532 ripley 38
#ifdef unix
53346 ripley 39
  \item{minimized, invisible}{arguments that are accepted on Windows but
40
    ignored on this platform, with a warning.}
40532 ripley 41
#endif
42
#ifdef windows
51190 ripley 43
  \item{minimized}{logical (not \code{NA}), indicates whether the
52801 ripley 44
    command window should be displayed initially as a minimized window.}
51190 ripley 45
  \item{invisible}{logical (not \code{NA}), indicates whether the
46
    command window should be visible on the screen.}
40532 ripley 47
#endif
15874 ripley 48
}
40532 ripley 49
\details{
52814 ripley 50
  Unlike \code{\link{system}}, \code{command} is always quoted by
53611 ripley 51
  \code{\link{shQuote}}, so it must be a single command without arguments.
61433 ripley 52
 
52814 ripley 53
  For details of how \code{command} is found see \code{\link{system}}.
15874 ripley 54
 
53700 hornik 55
  On Windows, \code{env} is currently only supported for commands such
53639 ripley 56
  as \command{R} and \command{make} which accept environment variables on
57
  their command line.
61433 ripley 58
 
53595 ripley 59
  Some Unix commands (such as \code{ls}) change their output depending
53576 ripley 60
  on whether they think it is redirected: \code{stdout = TRUE} uses a
61
  pipe whereas \code{stdout = "some_file_name"} uses redirection.
62
 
52814 ripley 63
  Because of the way it is implemented, on a Unix-alike \code{stderr =
53346 ripley 64
    TRUE} implies \code{stdout = TRUE}: a warning is given if this is
65
  not what was specified.
40532 ripley 66
}
53346 ripley 67
%% We use popen, and that pipes stdout only
15874 ripley 68
 
61433 ripley 69
\value{
52814 ripley 70
  If \code{stdout = TRUE} or \code{stderr = TRUE}, a character vector
71
  giving the output of the command, one line per character string.
72
  (Output lines of more than 8095 bytes will be split.)  If the command
53349 ripley 73
  could not be run an \R error is generated.  If \code{command} runs but
57469 ripley 74
  gives a non-zero exit status this will be reported with a warning and
75
  in the attribute \code{"status"} of the result: an attribute
76
  \code{"errmsg"} may also be available
40532 ripley 77
 
52814 ripley 78
  In other cases, the return value is an error code (\code{0}
40532 ripley 79
  for success), given the invisible attribute (so needs to be printed
80
  explicitly).  If the command could not be run for any reason, the
52802 ripley 81
  value is \code{127}.  Otherwise if \code{wait = TRUE} the value is the
82
  exit status returned by the command, and if \code{wait = FALSE} it is
83
  \code{0} (the conventional success value).
40532 ripley 84
#ifdef windows
52801 ripley 85
  Some Windows commands return out-of-range status values
52795 ripley 86
  (e.g. \code{-1}) and so only the bottom 16 bits of the value are used.
40532 ripley 87
#endif
88
}
89
 
52801 ripley 90
\note{
53346 ripley 91
  \code{system2} is a more portable and flexible interface than
92
  \code{\link{system}}, introduced in \R 2.12.0.  It allows redirection
53611 ripley 93
  of output without needing to invoke a shell on Windows, a portable way
94
  to set environment variables for the execution of \code{command}, and
53602 ripley 95
  finer control over the redirection of \code{stdout} and \code{stderr}.
53611 ripley 96
  Conversely, \code{system} (and \code{shell} on Windows) allows the
97
  invocation of arbitrary command lines.
52838 ripley 98
 
52859 ripley 99
  There is no guarantee that if \code{stdout} and \code{stderr} are both
100
  \code{TRUE} or the same file that the two streams will be interleaved
101
  in order.  This depends on both the buffering used by the command and
102
  the OS.
52801 ripley 103
}
104
 
52814 ripley 105
\seealso{
106
  \code{\link{system}}.
52801 ripley 107
#ifdef windows
61433 ripley 108
 
53602 ripley 109
  \code{\link{shell}} and \code{\link{shell.exec}}.
40532 ripley 110
#endif
38819 ripley 111
}
15874 ripley 112
\keyword{interface}
113
\keyword{file}
114
\keyword{utilities}