R more Unix-like?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

R more Unix-like?

Robert Citek

Is there a way to stream data into R so that it can be used in a  
command pipe?

Most Unix commands read data via standard input and read the script  
from the command line.  For example:

   $ seq 1 10 | sed -e 's/^/foo /'
   $ seq 1 10 | awk '{print "foo " $1}'
   $ seq 1 10 | perl -lane 'print "foo $F[0]"'

In contrast, R seems to read the script from standard input, thereby  
preventing streaming data through that file descriptor.  In reading  
through the man page for R and R-intro.html I haven't seen anything  
obvious to change this behaviour.  Is it possible?

Pointers to references would be greatly appreciated.

Regards,
- Robert
http://www.cwelug.org/downloads
Help others get OpenSource software.  Distribute FLOSS
for Windows, Linux, *BSD, and MacOS X with BitTorrent

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: R more Unix-like?

Gabor Grothendieck
Try this on Windows or the corresponding ls command on UNIX:

  readLines(pipe("cmd /c dir"))

On 5/10/06, Robert Citek <[hidden email]> wrote:

>
> Is there a way to stream data into R so that it can be used in a
> command pipe?
>
> Most Unix commands read data via standard input and read the script
> from the command line.  For example:
>
>   $ seq 1 10 | sed -e 's/^/foo /'
>   $ seq 1 10 | awk '{print "foo " $1}'
>   $ seq 1 10 | perl -lane 'print "foo $F[0]"'
>
> In contrast, R seems to read the script from standard input, thereby
> preventing streaming data through that file descriptor.  In reading
> through the man page for R and R-intro.html I haven't seen anything
> obvious to change this behaviour.  Is it possible?
>
> Pointers to references would be greatly appreciated.
>
> Regards,
> - Robert
> http://www.cwelug.org/downloads
> Help others get OpenSource software.  Distribute FLOSS
> for Windows, Linux, *BSD, and MacOS X with BitTorrent
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: R more Unix-like?

Robert Citek

On May 10, 2006, at 10:40 PM, Gabor Grothendieck wrote:

> Try this on Windows or the corresponding ls command on UNIX:
>
>   readLines(pipe("cmd /c dir"))

I tried this from within R:

   readLines(pipe("bash -c ls"))

and this from the command line:

   $ echo 'readLines(pipe("bash -c ls"))' | R -q --no-save

both of which produced a listing of the current folder.  So one  
strategy would be to build up the command pipe within the R program:

   $ echo 'for (i in readLines(pipe("seq 1 10")))
           print(sprintf("foo %s", i))' | R

While not exactly the solution I was looking for, it did allow me to  
do perform some simple summary stats on one field in a large data set:

$ wc -l ../data/foo.txt
13628161 ../data/foo.txt

$ echo 'summary(as.numeric(readLines(pipe("< ../data/foo.txt cut -f  
10 | tail +2")))); proc.time()' | R

R > summary(as.numeric(readLines(pipe("< ../data/foo.txt cut -f 10 |  
tail +2")))); proc.time()
    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
   114.5   862.8  1316.0  2030.0  2111.0 65540.0
[1] 59.323  0.964 60.283  8.324  8.324

All that in about a minute.  Very nice.

Regards,
- Robert
http://www.cwelug.org/downloads
Help others get OpenSource software.  Distribute FLOSS
for Windows, Linux, *BSD, and MacOS X with BitTorrent

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: R more Unix-like?

Steve Miller-4
In reply to this post by Robert Citek
You can use the pipe command to read data:

 

msci <- read.table(pipe("python steve/python/msciintl.py"),sep=",",header=T,
as.is=T)

 

I use this metaphor quite a bit for loading data.

 

Steve Miller

 

 

-----Original Message-----
From: [hidden email]
[mailto:[hidden email]] On Behalf Of Robert Citek
Sent: Wednesday, May 10, 2006 9:02 PM
To: [hidden email]
Subject: [R] R more Unix-like?

 

 

Is there a way to stream data into R so that it can be used in a  

command pipe?

 

Most Unix commands read data via standard input and read the script  

from the command line.  For example:

 

   $ seq 1 10 | sed -e 's/^/foo /'

   $ seq 1 10 | awk '{print "foo " $1}'

   $ seq 1 10 | perl -lane 'print "foo $F[0]"'

 

In contrast, R seems to read the script from standard input, thereby  

preventing streaming data through that file descriptor.  In reading  

through the man page for R and R-intro.html I haven't seen anything  

obvious to change this behaviour.  Is it possible?

 

Pointers to references would be greatly appreciated.

 

Regards,

- Robert

http://www.cwelug.org/downloads

Help others get OpenSource software.  Distribute FLOSS

for Windows, Linux, *BSD, and MacOS X with BitTorrent

 

______________________________________________

[hidden email] mailing list

https://stat.ethz.ch/mailman/listinfo/r-help

PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html