|
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? Thanks, -- Sergio. [[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 and provide commented, minimal, self-contained, reproducible code. |
|
Hi,
If you want to just count the words in R, try this: vec1<-c("uno dos tres", "cuatro cinco", "seis") #get individual word count within quotes res1<-unlist(lapply(strsplit(vec1, " "),length)) res1 #[1] 3 2 1 #get whole word count length(unlist(strsplit(vec1, " "))) #[1] 6 ----- Original Message ----- From: Julio Sergio Santana <[hidden email]> To: [hidden email] Cc: Sent: Wednesday, September 12, 2012 1:40 PM Subject: [R] Trying to use pipes in R 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? Thanks, -- Sergio. [[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 and provide commented, minimal, self-contained, reproducible code. ______________________________________________ [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 and provide commented, minimal, self-contained, reproducible code. |
|
No, this is not what I want, "wc" is just an example. What I want is to
shut a process to do a task outside R, and to get its results back in R On Wed, Sep 12, 2012 at 1:07 PM, arun <[hidden email]> wrote: > Hi, > > If you want to just count the words in R, try this: > vec1<-c("uno dos tres", "cuatro cinco", "seis") > #get individual word count within quotes > res1<-unlist(lapply(strsplit(vec1, " "),length)) > res1 > #[1] 3 2 1 > > #get whole word count > length(unlist(strsplit(vec1, " "))) > #[1] 6 > > > > > > ----- Original Message ----- > From: Julio Sergio Santana <[hidden email]> > To: [hidden email] > Cc: > Sent: Wednesday, September 12, 2012 1:40 PM > Subject: [R] Trying to use pipes in R > > 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? > > Thanks, > -- Sergio. > > [[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 > and provide commented, minimal, self-contained, reproducible code. > > -- Julio Sergio Santana Instituto Mexicano de Tecnología del Agua Tel +52 777 3293600 x 826 Tel/Fax +52 777 3293683 ------------------------------------------- [[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 and provide commented, minimal, self-contained, reproducible code. |
|
In reply to this post by Julio Sergio Santana
On 12/09/2012 1:40 PM, 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? The examples on the page don't show R both giving input and reading output from the same pipe. I don't think you can set it up that way; R is only one process. The usual approach is what is shown there: write to a temporary file, then pipe the results of a command that works on that file back into R. I guess you could also pipe some data into a command that writes a file, and then read that. Duncan Murdoch ______________________________________________ [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 and provide commented, minimal, self-contained, reproducible code. |
|
In reply to this post by Julio Sergio Santana
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. ______________________________________________ [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 and provide commented, minimal, self-contained, reproducible code. |
|
Thanks you all,
With your recomendations I built my solution as follows: :> writeLines(c("uno dos tres", "cuatro cinco", "seis"), "tempfile.txt") :> o <- pipe("wc < tempfile.txt", open="r") :> readLines(o) :[1] " 3 6 31" best regards, --Sergio. ______________________________________________ [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 and provide commented, minimal, self-contained, reproducible code. |
| Powered by Nabble | Edit this page |
