[R] use of "input" in system()

Duncan Murdoch murdoch at stats.uwo.ca
Fri Apr 24 16:34:12 CEST 2009


On 4/24/2009 10:29 AM, Mike Miller wrote:
> First, it looks like there is bug in the documentation...
> 
> According to the documentation for system():
> 
> http://stat.ethz.ch/R-manual/R-patched/library/base/html/system.html
> 
>    input  if a character vector is supplied, this is copied one string per
>           line to a temporary file, and the standard input of command is
>           redirected to the file
> 
> This seems to mean that the standard input of command is redirected *from* 
> the file.  From the file, to the command.  Example:

The redirection is done *to* the file handle.  The fact that input is 
read from that handle is a different issue.

Duncan Murdoch

> 
>> i <- 42
>> system( "cat -", input=as.character(i) )
> 42
> 
> I guess the documentation needs to be clarified because I couldn't 
> understand what it meant.
> 
> Second, I'm trying to use a system call to write files with names based on 
> an integer value in an R variable (in this case the variable is called 
> "i").  As above, I can do this (on a GNU/Linux system):
> 
>> system( "cat -", input=sprintf("%04d", i) )
> 0005
> 
> I am running bash, but system calls go to sh.  I want to be able to put 
> that input string ("0005") into a variable and do something like this:
> 
> system( "i=`cat /dev/stdin` ; run_script > file${i}.out" , input=sprintf("%04d", i) )
> 
> I think my problem is more with sh than with R, but someone here must have 
> tried this, so I'm hoping I can get an answer here.  Thanks in advance.
> 
> Best,
> Mike
>




More information about the R-help mailing list