[R] How to get 'R' to talk BACK to other languages / scripts??

Kjetil Halvorsen kjetilbrinchmannhalvorsen at gmail.com
Fri Dec 3 23:03:38 CET 2010


see below.

On Fri, Dec 3, 2010 at 3:23 PM, Mike Williamson <this.is.mvw at gmail.com> wrote:
> Hey everyone,
>
>    I know that I can call 'R' from other scripts, and that I can make
> command calls from 'R' (e.g., using system() ).  But how can I get 'R' to
> RETURN values to the script that called it.  E.g., I would like to be able
> to do something like the following (as a simpler example) from a bash
> script:
>
> #!/bin/bash
>
> myTest=echo /usr/local/bin/R --no-restore --no-save -f testing.R.r
>
> echo "myTest contains"
> echo $myTest
>
>
>    And ideally this should write out the results of the
> "testing.R.rscript".  So that if the testing.
> R.r script said something simple like:
>
> myResult <- paste("Hello World")
>
>    Then in the output of the bash script, it should say "myTest
> contains\nHello World" or something quite similar.  But instead it says
> myTest contains
> R version 2.11.1 (2010-05-31) Copyright (C) 2010 The R Foundation for
> Statistical Computing ISBN 3-900051-07-0 R is free software and comes with
> ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain
> conditions. Type 'license()' or 'licence()' for distribution details.
> Natural language support but running in an English locale R is a
> collaborative project with many contributors. Type 'contributors()' for more
> information and 'citation()' on how to cite R or R packages in publications.
> Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()'
> for an HTML browser interface to help. Type 'q()' to quit R. > > > myResult
> <- paste("Hello World") [1]
>
>    As you can see, the return from the bash call is really just capturing
> everything in the ST OUT once 'R' is called, which is not what I want.  I
> have tried several variations, using a function - return combination, etc.
> I have also looked through help pages & documentation.  As far as I can
> tell, it seems no one ever bothers to get information OUT of 'R' and into
> other scripts.  I could write to a csv file & read that file, but that seems
> a REALLY clunky way to handle variable passing.
>

Look at this example:

kjetil at kjetil:~$ R --vanilla < test.R > outtest.txt
kjetil at kjetil:~$ cat outtest.txt

R version 2.13.0 Under development (unstable) (2010-11-26 r53672)
Copyright (C) 2010 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: x86_64-unknown-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

>  test <- 1:10
>  test <- sample(test)
>  cat(file=stdout(), test, "\n")
3 10 9 7 2 4 6 5 1 8
> q("no")
kjetil at kjetil:~$ cat test.R
 test <- 1:10
 test <- sample(test)
 cat(file=stdout(), test, "\n")
q("no")

kjetil at kjetil:~$






>                                Thanks for any help!
>                                          Regards,
>                                                    Mike
>
>
>
>
> "Telescopes and bathyscaphes and sonar probes of Scottish lakes,
> Tacoma Narrows bridge collapse explained with abstract phase-space maps,
> Some x-ray slides, a music score, Minard's Napoleanic war:
> The most exciting frontier is charting what's already here."
>  -- xkcd
>
> --
> Help protect Wikipedia. Donate now:
> http://wikimediafoundation.org/wiki/Support_Wikipedia/en
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org 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