Skip to content

using the system command

1 message · William Dunlap

#
system(wait=FALSE, command) means to return to R without
waiting for the command to finish.  E.g., on Linux with
the following function

  f <- function (seconds, wait) {
      tf <- tempfile()
      on.exit(unlink(tf))
      system(sprintf("(sleep %d ; date) > '%s'", seconds, tf),
          wait = wait)
      readLines(tf)
  }

we get

  > f(3, wait=FALSE)
  character(0)
  > f(3, wait=TRUE)
  [1] "Wed Sep 28 10:23:33 PDT 2011"

In 10000 calls to f(seconds=0, wait=FALSE), 9915 resulted in character(0),
meaning that the shell's processing of "> file" had created the file
but date had not yet put any text into it, 81 resuled in an error
from readLines that the file did not not exist yet, and 4 succeeded
in reading the date from the file.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com