Rev 44615 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/base/man/system.Rd% Part of the R package, http://www.R-project.org% Copyright 1995-2007 R Core Development Team% Distributed under GPL 2 or later\name{system}\alias{system}\title{Invoke a System Command}\description{\code{system} invokes the OS command specified by \code{command}.}\usage{system(command, intern = FALSE, ignore.stderr = FALSE,wait = TRUE, input = NULL, show.output.on.console = TRUE,minimized = FALSE, invisible = TRUE)}\arguments{\item{command}{the system command to be invoked, as a string.}\item{intern}{a logical (not \code{NA}) which indicates whether tomake the output of the command an \R object.#ifdef unixNot available unless \code{popen} is supported on the platform.#endif}\item{ignore.stderr}{a logical indicating whether error messages writtento \file{stderr} should be ignored.}\item{wait}{a logical indicating whether the \R interpreter shouldwait for the command to finish, or run it asynchronously.This will be ignored (and the interpreter will always wait) if\code{intern = TRUE}.}\item{input}{if a character vector is supplied, this is copied onestring per line to a temporary file, and the standard input of\code{command} is redirected to the file.}#ifdef unix\item{show.output.on.console, minimized, invisible}{argumentsthat are accepted on other platforms but ignored on this one, with awarning.}#endif#ifdef windows\item{show.output.on.console}{a logical, indicates whether to capturethe output of the command and show it on the \R console (not usedby \code{Rterm}, which captures the output unless \code{wait}is false).}\item{minimized}{a logical, indicates whether the command windowshould be initially displayed as a minimized window.}\item{invisible}{a logical, indicates whether the command windowshould be visible on the screen.}#endif}\details{\code{command} is parsed as a command plus arguments separated by spaces.So if the path to the command (or a filepath argument) containsspaces, it must be quoted e.g. by \code{\link{shQuote}}.#ifdef windowsOnly double quotes are allowed on Windows: see the examples. (Note: aWindows path name cannot contain a double quote, so we do not need toworry about escaping embedded quotes.)#endifHow the command is run differs by platform: Unix-alikes use a shell(\file{/bin/sh} by default), and Windows executes the command directly(extensions \file{.exe}, \file{.com}) or as a batch file (extensions\file{.cmd} and \file{.bat}).#ifdef windows(These extensions are tried in turn if none is supplied.)This means that redirection, pipes, \dots cannot be used: see\code{\link{shell}}. (To use DOS internal commands use\code{paste(Sys.getenv("COMSPEC"),"/c", cmd)}.)The search path for \code{command} may be system-dependent: it willinclude the \R \file{bin} directory, the working directory and theWindows system directories before \code{PATH}.#endif#ifdef unixIf \code{intern} is \code{TRUE} then \code{popen} is used to invoke thecommand and the output collected, line by line, into an \R\code{\link{character}} vector. If \code{intern} is \code{FALSE} thenthe C function \code{system} is used to invoke the command.#endifThe ordering of arguments after the first two has changed from time totime: it is recommended to name all arguments after the first.}#ifdef unix\section{Stdout and stderr}{Error messages written to \file{stderr} will be sent by the shell tothe terminal unless \code{ignore.stderr = TRUE}. They can be captured(in the most likely shells) by\preformatted{system("some command 2>&1", intern=TRUE)}What happens to output sent to \file{stdout} and \file{stderr} if\code{intern = FALSE} is interface-specific, and it is unsafe toassume that such messages will appear on the console (they do on theMacOS X console but not on the \pkg{gnomeGUI} console, for example).}\note{\code{wait} is implemented by appending \code{&} to the command: thisis shell-dependent, but required by POSIX and so widely supported.}#endif#ifdef windows\section{Interaction with the command}{Precisely what is seen by the user depends on whether \code{Rgui} or\code{Rterm} is being used. For \code{Rgui} a new \sQuote{console}will always be used, so a commands window will appear for the durationof console applications unless \code{invisible} is true (which it isby default). For \code{Rterm} a separate commands window will appearfor console applications only if \code{wait = FALSE} and\code{invisible = FALSE}.If the \R process is waiting for output from the command it ispossible to interrupt the running command from the keyboard or\code{Rgui} menu: this should at least return control to the \Rconsole. Otherwise it it not possible to interrupt the runningcommand.Do not try to run console applications that require userinput from \code{Rgui} setting \code{intern = TRUE} and/or\code{show.output.on.console = TRUE}. They will not work,may hang and then will probably hang \code{Rgui} too.}#endif\value{If \code{intern = TRUE}, a character vector giving the output of thecommand, one line per character string. (Output lines of more than8095 characters will be split.) If the command could not be run orgives an error#ifdef unixthis will be reported on the shell's \file{stderr} (unless\code{popen} is not supported, when there is an \R error).#endif#ifdef windowsan \R error is generated.#endif#ifdef windowsThis also captures \code{stderr} on the RGui console unless\code{ignore.stderr = TRUE}.#endifIf \code{intern = FALSE}, the return value is an error code (\code{0}for success), given the invisible attribute (so needs to be printedexplicitly). If the command could not be run for any reason, thevalue is#ifdef unix\code{256*127 = 52512}.#endif#ifdef windows\code{-1}. (An \R warning is also generated.)#endifOtherwise if \code{wait = TRUE} the value is#ifdef unix256 times#endifthe error code returned by the command, and if \code{wait = FALSE} itis \code{0} (the conventional success value).#ifdef windowsSome Windows commands return out-of-range error values (e.g. -1) and soonly the bottom 16 bits of the value are used.If \code{intern = FALSE} and \code{show.output.on.console = TRUE}the \file{stdout} and \file{stderr} (unless \code{ignore.stderr =TRUE}) output from a command that is a console application shouldappear in the \R console (\code{Rgui}) or the window running \R(\code{Rterm}).Not all Windows executables properly respect redirection of output, ormay only do so from a console application such as \code{Rterm} and notfrom \code{Rgui}: known examples include \file{fc.exe}.#endif}\seealso{#ifdef windows\code{\link{shell}} or \code{\link{shell.exec}} for a less rawinterface.#endif\code{\link{.Platform}} for platform-specific variables.}#ifdef unix\examples{# list all files in the current directory using the -F flag\dontrun{system("ls -F")}# t1 is a character vector, each one# representing a separate line of output from who# (if the platform has popen and who)t1 <- try(system("who", intern = TRUE))try(system("ls fizzlipuzzli", intern = TRUE, ignore.stderr = TRUE))# empty since file doesn't exist}#endif#ifdef windows\examples{# launch an editor, wait for it to quit\dontrun{system("notepad myfile.txt")}# launch your favourite shell:\dontrun{system(Sys.getenv("COMSPEC"))}\dontrun{## note the two sets of quotes here:system(paste('"c:/Program Files/Mozilla Firefox/firefox.exe"','-url cran.r-project.org'), wait = FALSE)}}#endif\keyword{interface}\keyword{file}\keyword{utilities}