[R] use of "input" in system()
Mike Miller
mbmiller+l at gmail.com
Fri Apr 24 20:28:09 CEST 2009
On Fri, 24 Apr 2009, Mike Miller wrote:
> On Fri, 24 Apr 2009, Mike Miller wrote:
>
>> 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) )
>
>
> In sh, both of these commands execute ls:
>
> echo ls | `cat /dev/stdin`
> echo ls | `cat -`
>
> In R, both of these simply hang until I hit ctrl-c...
>
>> system( "`cat /dev/stdin`", input="ls" )
>> system( "`cat -`", input="ls" )
>
> ...but the stdin is getting to the command because both of these return
> "ls" to stdout:
>
>> system( "cat -", input="ls" )
>> system( "cat /dev/stdin", input="ls" )
>
>
> So what am I missing? I don't understand why the backtick method isn't
> working. Is there another way to do this?
I still can't figure that out, but it is OK because I found a better way.
For some reason I was thinking of the system function like this...
system( "command" )
...when I could have been thinking of it like this:
foo <- "command"
system( foo )
Maybe obvious in retrospect, but that makes it much easier to take values
from R and insert them into system commands. Here is a mini example:
> i <- 5
> foo <- "ls"
> bar <- paste("touch file", sprintf("%04d", i), ".out", sep="")
> bar
[1] "touch file0005.out"
Thus I have placed two system commands into variables foo and bar. The
system commands can be pasted together using " ; " as a separator...
> barfoo <- paste(bar, foo, sep=" ; ")
> barfoo
[1] "touch file0005.out ; ls"
...then executed like so:
> system( barfoo )
Mike
Michael B. Miller, Ph.D.
Minnesota Center for Twin and Family Research
Department of Psychology
University of Minnesota
More information about the R-help
mailing list