[R] Trying to use pipes in R

Petr Savicky savicky at cs.cas.cz
Wed Sep 12 20:40:16 CEST 2012


On Wed, Sep 12, 2012 at 12:40:54PM -0500, Julio Sergio Santana wrote:
> Hi,
> I'm trying to use pipes in R. By now, I could launch the linux command "wc"
> (to count words from a text), but
> I don't know how to capture the results, say in a vector of chars...
> Here is the R code I'm trying:
> 
> :> f <- pipe("wc", open="w")
> :> writeLines(c("uno dos tres", "cuatro cinco", "seis"), f)
> :> close(f)
> :     3       6      31
> 
> The result is just displayed but I couldn't put the result into a R
> variable.
> Do you have any comments on this?

Hi.

Try the following.

  writeLines(c("uno dos tres", "cuatro cinco", "seis"), "some_file.txt")
  out <- system("wc some_file.txt", intern=TRUE)
  out

  [1] " 3  6 31 some_file.txt"

Here, out is a character variable.

Hope this helps.

Petr Savicky.




More information about the R-help mailing list