Skip to content
Prev 178960 / 398506 Next

use of "input" in system()

On Fri, 24 Apr 2009, Duncan Murdoch wrote:

            
Thanks very much for the reply.  After seeing your response, I'm sure the 
document isn't wrong (not a bug) but it is a very terse explanation that I 
think many people will find hard to follow, as I did.

First, a temporary file is created.  Where is it?  What is it called?  If 
the name/location don't matter, does it even matter that a temporary file 
is created?  Is this just info about the internal workings of R that the 
user doesn't need to know?

Then "the standard input of command is redirected to the file".  I think I 
get this now.  I think it means that this is being done behind the scenes:

command < file

Would it help users to tell them that, if that is correct?  It would have 
helped me.  I think this is basically what it is doing:

# For input to the system command we'll need a command and a vector:
command <- "any command string"
v <- c("some", "vector")

# make a temp file:
filename <- tempfile( tmpdir=tempdir() )
write.table(v, file=filename, sep="\n", row.names=FALSE, col.names=FALSE, quote=FALSE)

# Then I think these two system commands do the same thing:

system( paste(command, "<", filename, sep=" ") )

system( command, input=v )

It would be nice if there were more documentation so that I could 
understand this better.  I wasn't understanding why system() hangs 
sometimes as in the simple examples below, but I think I get it now:  The 
stdout goes only to the last command in a string of semi-colon-separated 
commands.  If that's right, it makes sense.

Mike


This works:
foo


This hangs until I hit ctrl-c:
This works:
bar
foo


This hangs until I hit ctrl-c:
Best,
Mike