[R] How to use bash command in R script?

Peter Dalgaard p.dalgaard at biostat.ku.dk
Tue Feb 27 07:37:38 CET 2007


Guo Wei-Wei wrote:
> Dear All:
>
> Maybe it is a too basic question, but I don't how to find the answer.
> Sorry for that.
>
> What I want to do is call a shell command, which will provide two
> numbers, and assign those numbers to a vector. For example:
>
> The following command:
>
> $mxresult.sh ABC.mx
>
> mxresult.sh is a script written by myself and ABC.mx is a Mx script.
> I can get two numbers, 126.128 and 29, with this command.
>
> Is there any way to do it like this:
>
> c <- somefunction("mxresult.sh ABC.mx")
>
> Or is their any other way to fulfill the function?
>   
txt <- system(""mxresult.sh ABC.mx", intern=TRUE)

is the first step.  Then you need to get the numbers using either a 
scan() on a textConnection (see its help page) or something like

mynum <- as.numeric(strsplit(txt, "  *")[[1]])


> Thanks in advance!
>
> Best washes,
> Wei-Wei
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>



More information about the R-help mailing list