[R] using the system command

William Dunlap wdunlap at tibco.com
Wed Sep 28 19:32:48 CEST 2011


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 

> -----Original Message-----
> From: Data Analytics Corp. [mailto:walt at dataanalyticscorp.com]
> Sent: Wednesday, September 28, 2011 10:19 AM
> To: William Dunlap
> Subject: Re: [R] using the system command
> 
> Hi William,
> 
> When I use wait = TRUE I get a lot of intermediate commands from
> StatTransfer.  For example, if the files it's transferring to already
> exist, it shows repeated messages asking if I want to overwrite.  wait =
> FALSE seems to avoid this.
> 
> Thanks,
> 
> Walt
> 
> ________________________
> 
> Walter R. Paczkowski, Ph.D.
> Data Analytics Corp.
> 44 Hamilton Lane
> Plainsboro, NJ 08536
> ________________________
> (V) 609-936-8999
> (F) 609-936-3733
> walt at dataanalyticscorp.com
> www.dataanalyticscorp.com
> _____________________________________________________
> 
> On 9/28/2011 1:09 PM, William Dunlap wrote:
> > Why do you use wait=FALSE in the call to system()?
> > Do things work differently without it?
> >
> > Bill Dunlap
> > Spotfire, TIBCO Software
> > wdunlap tibco.com
> >
> >> -----Original Message-----
> >> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Data
> Analytics
> >> Corp.
> >> Sent: Wednesday, September 28, 2011 9:05 AM
> >> To: r-help at r-project.org
> >> Subject: [R] using the system command
> >>
> >> Hi,
> >>
> >> I started playing around with a function for using StatTransfer (version
> >> 10) for importing data.  This started as a simple task but it's not
> >> working and so now I'm very frustrated.  I'm using R version 2.13 on
> >> Windows 7.
> >>
> >> The function, called fn.importData,  is:
> >>
> >>           function(file = NULL, type = NULL){
> >>               ##
> >>               ##  create statTransfer command file - stcmd
> >>               ##
> >>               ext<- switch(type, sas = "sas7bdat", excel = "xls")
> >>               tmp<- paste("copy C:\\Temp\\", file, ".", ext, " r
> >> c:\\temp\\", file, ".rdata -T>  ", file, " \n\nquit", sep = "")
> >>               cat(tmp, file = "c:/temp/transfer.stcmd", append = FALSE)
> >>               ##
> >>               ##  transfer using StatTransfer
> >>               ##
> >>               system(paste('"c:\\program files\\statTransfer10\\st.exe"',
> >> 'c:\\temp\\transfer.stcmd'), wait = FALSE)
> >>               ##
> >>               ##  load
> >>               ##
> >>               inpt<- paste("c:\\temp\\", file, ".RData", sep = "")
> >>               ##return(inpt)
> >>               load(inpt, .GlobalEnv)
> >>          }
> >>
> >> My call using a sas file (vicks.sasa7bdat) is:
> >>
> >>           fn.importData(file = "vicks", type = "sas")
> >>
> >> The StatTransfer command file is created and StatTransfer is called
> >> correctly.  The translation from vicks.sas7bdat  to vicks.RData occurs
> >> as it should - I can look at them in the temp directory.  But I get an
> >> error message for the load function:
> >>
> >>           Error in readChar(con, 5L, useBytes = TRUE) : cannot open the
> >> connection
> >>           In addition: Warning message:
> >>           In readChar(con, 5L, useBytes = TRUE) :
> >>           cannot open compressed file 'c:\temp\vicks.RData', probable
> >> reason 'No such file or directory'
> >>
> >> If I uncomment the return(inpt) statement in the above function and use
> >> the following at the command line:
> >>
> >>           load(fn.importData(file = "vicks", type = "sas"))
> >>
> >> then everything works fine.  But of course I don't want to do this.  Any
> >> suggestions as to what I'm doing wrong?  Why the error message?
> >>
> >> Also, the StatTransfer program sometimes fails to close so if I
> >> repeatedly call my function I'll have multiple occurrences of
> >> StatTransfer running.  How can I force it to close after the transfer?
> >>
> >> Thanks,
> >>
> >> Walt
> >>
> >> ________________________
> >>
> >> Walter R. Paczkowski, Ph.D.
> >> Data Analytics Corp.
> >> 44 Hamilton Lane
> >> Plainsboro, NJ 08536
> >> ________________________
> >> (V) 609-936-8999
> >> (F) 609-936-3733
> >> walt at dataanalyticscorp.com
> >> www.dataanalyticscorp.com
> >>
> >> ______________________________________________
> >> R-help at r-project.org mailing list
> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> >> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list